跳到正文

题解

[USACO17JAN] Cow Dance Show S 题解

Ame_wiki约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P3611

版本

  1. v1当前

题目传送门:link。

思路:二分答案,用 multiset 维护二分(去重),可以看成有 kk 个位置,最终答案就是最大的位置上时间和。所以 kk 次后插入时就把前面累计的时间加上即可,判断与 TmaxT_{max} 大小关系再二分即可。

如下:普通 set 只能得 3030 分:

故用 multiset 去重。

code

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

const int N=1e5+5,M=5005,inf=1e18;

int n,a[N],tmax;

int ck(int k){
	multiset<int> st;
	int tim=0;
	for(int i=1;i<=n;i++){
		if(i<=k){
			st.insert(a[i]);
		}
		else{
			int tmp=*st.begin();
			st.erase(st.begin());
			st.insert(tmp+a[i]);
		}
	}
	int maxx=-inf;
	for(auto v:st){
		maxx=max(maxx,v);
	}
	return (maxx<=tmax);
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n>>tmax;
	for(int i=1;i<=n;i++) cin>>a[i];
	int l=0,r=n+1;
	while(l+1!=r){
		int mid=(l+r)>>1;
//		cout<<mid<<": "<<ck(mid)<<endl;
		if(ck(mid)) r=mid;
		else l=mid;
	}
	cout<<r;
	return 0;
}
原文