跳到正文

个人记录

3.28 周六下午--贪心训练2--二分进阶

WUzongze约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年3月28日
收录
2026年3月28日
更新
2026年11月9日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

比赛信息

比赛名称:3.28 周六下午—贪心训练2—二分进阶

时间:2026.3.28 周六下午

链接:3.28 周六下午—贪心训练2—二分进阶

题面下载:贪心训练2 题面

题解下载:暂无题解下载

题目

[CCC 2019 J4/S1] Flipper 翻转

题号:P15413

链接:[CCC 2019 J4/S1] Flipper 翻转

思路

模拟

代码
C++
#include <bits/stdc++.h>
using namespace std;
int aa = 1,ab = 2,ba = 3,bb = 4;
int main(){
	string s;
	cin>>s;
	for (int i = 0;i < s.size();i++){
		if (s[i] == 'H'){
			int aaw,abw,baw,bbw;
			aaw = ba;
			abw = bb;
			baw = aa;
			bbw = ab;
			ab = abw;
			bb = bbw;
			aa = aaw;
			ba = baw;
		}
		else{
			int aaw,abw,baw,bbw;
			aaw = ab;
			abw = aa;
			baw = bb;
			bbw = ba;
			ab = abw;
			bb = bbw;
			aa = aaw;
			ba = baw;
		}
	}
	cout<<aa<<' '<<ab<<endl<<ba<<' '<<bb;
	return 0;
}
ERROR

这道题的满分是15分\textcolor{red}{\textup{\textbf{这道题的满分是15分}}}

[NOISG 2016] LunchBox

题号:P13646

链接:[NOISG 2016] LunchBox

思路

先排序,优先发给k值小的学校

代码
C++
#include <bits/stdc++.h>
using namespace std;
int k[60000];
int main(){
	int n,m,sum = 0,l = 0;
	cin>>n>>m;
	for (int i = 1;i <= m;i++){
		cin>>k[i];
	}
	sort(k + 1,k + m + 1);
	for (int i = 1;i <= m;i++){
		if (n - sum < k[i]){
			break;
		}
		else{
			sum += k[i];
			l++;
		}
	}
	cout<<l;
	return 0;
}

[UOI 2020 II Stage] 修理帕萨特

题号:P15024

链接:[UOI 2020 II Stage] 修理帕萨特

思路

T3

代码
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
pair <ll,ll> a[N];
pair <ll,ll> tmp[N];
int n;
bool check(ll k){
	ll sum_t = k;
	for (int i = 1;i <= n;i++){
		sum_t += a[i].second;
		if (sum_t > a[i].first) return false;
	}
	return true;
}
int main(){
	cin>>n;
	ll minn = 1e18;
	for (int i = 1;i <= n;i++){
		cin>>a[i].second>>a[i].first;
		minn = min(minn,a[i].first);
	}
	sort(a + 1,a + 1 + n);
	ll L = -1,R = minn + 1;
	while (L + 1 < R){
		ll mid = L + (R - L) / 2;
		if (check(mid)){
			L = mid;
		}
		else{
			R = mid;
		}
	}
	cout<<L<<endl;
	return 0;
}

[NOI Online #2 入门组] 未了

题号:P6473

链接:[NOI Online #2 入门组] 未了

思路

T4

代码
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N];
ll sum[N];
ll Len,v,n,q;
bool cmp(ll x,ll y){
	return x > y;
}
bool check(ll k,ll t){
	ll tot = Len;
	tot += sum[k];
	return tot > t * v;
}
int main(){
	cin>>n>>Len>>v;
	for (int i = 1;i <= n;i++){
		cin>>a[i];
	}
	sort (a + 1,a + 1 + n,cmp);
	for (int i = 1;i <= n;i++){
		sum[i] = sum[i - 1] + a[i];
	}
	cin>>q;
	while (q--){
		ll t;
		cin>>t;
		int l = 0 - 1,r = n + 1;
		while (l + 1 < r){
			int mid = (l + r) / 2;
			if (check(mid,t)){
				r = mid;
			}
			else{
				l = mid;
			}
		}
		if (sum[r] + Len > t * v) cout<<r<<endl;
		else cout<<-1<<endl;
	}
	return 0;
}

[USACO17JAN] Cow Dance Show S

题号:P3611

链接:[USACO17JAN] Cow Dance Show S

思路

T5

代码
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n,T,a[N];
bool check(int k){
	priority_queue<ll,vector<ll>,greater<ll> >pq;
	for (int i = 1;i <= k;i++){
		pq.push(a[i]);
	}
	for (int i = k + 1;i <= n;i++){
		pq.push(pq.top() + a[i]);
		pq.pop();
	}
	ll ans = 0;
	while (pq.size()){
		ans = pq.top();
		pq.pop();
	}
	return ans <= T;
}
int main(){
	cin>>n>>T;
	for (int i = 1;i <= n;i++){
		cin>>a[i];
	}
	ll l = 0,r = n + 1;
	while (l + 1 < r){
		ll mid = (l + r) / 2;
		if (check(mid)) r = mid;
		else l = mid;
	}
	cout<<r;
	return 0;
}

原文