
D. Serval and Kaitenzushi Buffet 【Codeforces Round 1011 (Div. 2)】
D. Serval and Kaitenzushi Buffet 【Codeforces Round 1011 (Div. 2)】数据结构(data structures)图匹配(graph matchings)贪心(greedy)
D. Serval and Kaitenzushi Buffet
思路:
很诈骗的题,一眼让人以为是DP。
其实只需要贪心即可。因为观察题目限制,可以发现吃每盘寿司需要的时间居然是一样的,那肯定优先考虑拿美味值高的。不难发现,任凭你怎么努力,最多也只能吃掉
n
/
(
k
+
1
)
n/(k+1)
n/(k+1) 盘寿司,并且拿倒数第
i
i
i盘寿司的时间不能晚于
(
n
−
i
⋅
(
k
+
1
)
+
1
)
(n - i\cdot(k+1) + 1)
(n−i⋅(k+1)+1) 。
(想想是不是很有道理,拿最后一盘寿司的时间一定不能晚于
n
−
k
n-k
n−k ,不然吃不完了)
维护一个优先队列,按序遍历时间,当当前时间不得不拿一盘寿司时,拿堆顶的这盘美味值最大的就行。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define int long long
#define pb push_back
#define pii pair<int,int>
#define FU(i, a, b) for(int i = (a); i <= (b); ++ i)
#define FD(i, a, b) for(int i = (a); i >= (b); -- i)
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
int d[200005];
void solve() {
priority_queue<int> qu;
int n,k;
cin>>n>>k;
int yz=(n-k)%(k+1);
FU(i,1,n){
cin>>d[i];
}
int ans = 0 ;
FU(i,1,n){
qu.push(d[i]);
if(i%(k+1)==yz){
ans+=qu.top();
qu.pop();
}
}
cout<<ans<<endl;
}
signed main() {
cin.tie(0)->ios::sync_with_stdio(0);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
更多推荐
所有评论(0)