引言:
作者认为贪心训练价值比较高,所以既想自己练也想拉几个人一起练,于是有了这篇文章。
PS:顺便展示优良码风。
B4071 [GESP202412 五级] 武器强化
思路
观察到 ,考虑暴力处理每一种情况。
对于情况武器 的数量为 的,可以计算需要花的钱,显然发现其他的武器数量都是小于等于 的,对于每一种情况都计算一遍,不够的就再补上。
时间复杂度 ,可以接受。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1005;
vector<int> v[NA];
int s[NA];
int sum[NA][NA];
int n,m;
int k(int x){
int res=0,cnt=s[1];
for(int i=2;i<=n;i++){
res+=sum[i][max(s[i]-x+1,0ll)];
cnt+=max(s[i]-x+1,0ll);
}
if(cnt<x){
priority_queue<int,vector<int>,greater<int> > q;
for(int i=2;i<=n;i++){
for(int j=max(s[i]-x+2,1ll);j<=s[i];j++){
q.push(v[i][j-1]);
}
}
while(!q.empty()&&cnt<x){
cnt++;
res+=q.top();
q.pop();
}
}
return res;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int p,c;
cin>>p>>c;
v[p].push_back(c);
}
for(int i=1;i<=n;i++){
sort(v[i].begin(),v[i].end());
s[i]=v[i].size();
for(int j=1;j<=s[i];j++){
sum[i][j]=sum[i][j-1]+v[i][j-1];
}
}
int ans=1e18;
for(int i=1;i<=m;i++){
ans=min(ans,k(i));
}
cout<<ans;
return 0;
} B4091 [CSP-X2020 山东] 分糖果
思路
显然不满足的情况为:
- 前一个比当前大,但糖果数小于等于前一个。
- 前一个比当前小,但糖果数大于等于前一个。
显然我们可以先令每一人的糖果为 ,贪心的想到如果情况不符合就令其为较大数加 ,时间复杂度为 。
code
#include<bits/stdc++.h>
using namespace std;
const int NA=1e5+5;
int a[NA],b[NA];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=1;
}
while(1){
int last=1,id=2;
bool flag=0;
for(int i=1;i<=n;i++){
if(a[id]>a[last]&&b[id]<=b[last]){
b[id]++;
flag=1;
}
if(a[id]<a[last]&&b[id]>=b[last]){
b[last]++;
flag=1;
}
last=id;
id++;
if(id==n+1) id=1;
}
if(flag==0) break;
}
int ans=0;
for(int i=1;i<=n;i++){
ans+=b[i];
}
cout<<ans;
return 0;
}B4107 [CSP-X2024 山东] 刷题
思路
显然的发现,题目要求了要按照题目编号顺序做完所有的题目。
考虑二分答案,显然我们对于每一天:能加就加,不能加就下一天,然后每次贪心的想,问小明当然是问最难的了。
思路完了,时间复杂度大概 。
code
#include<bits/stdc++.h>
using namespace std;
const int NA=1e5+5;
int a[NA];
int n,m;
bool check(int k){
int id=1;
for(int i=1;i<=m;i++){
int maxn=0,sum=0;
while(id<=n&&sum+a[id]-max(maxn,a[id])<=k){
sum+=a[id];
maxn=max(maxn,a[id]);
id++;
}
if(id>n){
return 1;
}
}
return 0;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int l=0,r=1e9;
while(l<r){
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<l<<"\n";
return 0;
}B4204 [常州市赛 2021] 烧菜
思路
类似于纯模拟,唯一需要的就是,我们每次必然让结束时间更早的干更多的活,而且模拟就是先洗再煮,题目里有写。
显然用小根堆来动态维护,时间复杂度 。
code
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,a,b;
cin>>n>>m>>a>>b;
priority_queue<int,vector<int>,greater<int> > q1,q2;
for(int i=1;i<=m;i++){
q1.push(0);
}
int cnt1=0,cnt2=0;
while(cnt1<n||cnt2<n){
if(cnt1<n){
int t=q1.top();
q1.pop();
t+=a;
q1.push(t);
q2.push(t);
cnt1++;
}
else if(cnt2<n){
int t=max(q1.top(),q2.top());
q1.pop();
q2.pop();
t+=b;
q1.push(t);
cnt2++;
}
}
int maxn=0;
while(!q1.empty()){
maxn=max(maxn,q1.top());
q1.pop();
}
cout<<maxn;
return 0;
}B4220 [常州市赛 2023] 洗牌
思路
发现可以将绝对值拆开来,我们显然能发现,加的数和减的数数量相同,所以希望让大的数为加的,小的数为减的。
然而两端的数字只算一次,因此必然是去掉加的最少的和减的最多的,处理到奇数时特判一下,仅提供思路,不予过多提示。
时间复杂度 ,记得开 long long。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e5+5;
int a[NA];
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
int ans=0;
for(int i=1;i<=n/2;i++){
ans-=a[i]*2;
}
for(int i=n/2+1;i<=n;i++){
ans+=a[i]*2;
}
if(n%2==0){
ans+=a[n/2];
ans-=a[n/2+1];
}
else{
if(a[n/2+1]-a[n/2]>a[n/2+2]-a[n/2+1]){
ans-=a[n/2+1];
ans-=a[n/2+2];
}
else{
ans-=a[n/2+1]*3;
ans+=a[n/2];
}
}
cout<<ans;
return 0;
}B4253 [科大国创杯小学组 2024] 几何
思路
分情况讨论:
- 当 时,我们用 的复杂度找最大值即可。
- 当 时,显然可以映射到四个象限,显然映射后距离不变,最大距离的那一组,必然再某个情况中大点横纵坐标大于小点横纵坐标,此时我们就好算了,复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e6+5;
struct node{
int x,y;
void read(){
cin>>x>>y;
}
}a[NA];
int n,op;
int get_ans(){
int maxn=-1e18,minn=1e18;
for(int i=1;i<=n;i++){
maxn=max(maxn,a[i].x+a[i].y);
minn=min(minn,a[i].x+a[i].y);
}
return maxn-minn;
}
signed main(){
cin>>n>>op;
for(int i=1;i<=n;i++){
a[i].read();
}
if(op==1){
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
ans=max(ans,(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
}
}
cout<<ans<<"\n";
}
if(op==2){
int ans=0;
ans=max(ans,get_ans());
for(int i=1;i<=n;i++){
a[i].x=-a[i].x;
}
ans=max(ans,get_ans());
for(int i=1;i<=n;i++){
a[i].y=-a[i].y;
}
ans=max(ans,get_ans());
for(int i=1;i<=n;i++){
a[i].x=-a[i].x;
}
ans=max(ans,get_ans());
cout<<ans<<"\n";
}
return 0;
}B4304 [蓝桥杯青少年组省赛 2024] 通关游戏的最少能量值
思路
邻项交换题。
最开始一眼可能想先按照 从大到小排序, 从小到大排序的方式来处理,但是只有 分。我们贪心地想,我们可以先处理开启分减去消耗分从大到小排序,我们先处理余量大的显然优于处理余量少的。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e5+5;
struct node{
int x,y;
void read(){
cin>>x>>y;
}
}a[NA];
int n;
bool cmp(node x,node y){
return x.x-x.y>y.x-y.y;
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
a[i].read();
}
sort(a+1,a+n+1,cmp);
int sum=0,ans=0;
for(int i=1;i<=n;i++){
if(ans-sum<a[i].x){
ans=a[i].x+sum;
}
sum+=a[i].y;
}
cout<<ans;
return 0;
}B4391 [常州市赛 2025] 拖拉机
思路
个人其实不算是贪心,既然来了就写一下。
显然我们只需要模拟这个操作即可,所以仅为锻炼模拟能力。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
int f1=min(b,e);
int f2=min(d,e);
int f=f1+f2;
b-=f1;
d-=f2;
int w1=min(a,f/2),w2=min(c,f/2);
a+=w1;
c+=w2;
f-=(w1+w2);
if(f1>f2){
int w=min(f1-f2,f);
b+=w;
f1-=w;
f-=w;
}
if(f1<f2){
int w=min(f2-f1,f);
d+=w;
f2-=w;
f-=w;
}
b+=f/2;
d+=f/2;
cout<<a<<" "<<b<<" "<<c<<" "<<d;
return 0;
}B4399 [蓝桥杯青少年组国赛 2025] 第四题
思路
我们可以考虑将 从大到小排序,将 从大到小排序,显然我们每次处理一个区间时,如果前面选的点它包含了,那么不管,否则我们将这个区间的 加入即可,显然我们加小的不仅便宜,而且更有可能被包含,这就是这道题的贪心。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e5+5;
struct node{
int l,r;
void read(){
cin>>l>>r;
}
}a[NA];
bool cmp(node x,node y){
if(x.l!=y.l) return x.l>y.l;
return x.r>y.r;
}
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
a[i].read();
}
sort(a+1,a+n+1,cmp);
int ans=0,last=1e18;
for(int i=1;i<=n;i++){
if(last>a[i].r){
ans+=a[i].l;
last=a[i].l;
}
}
cout<<ans;
return 0;
}B4514 [四川青少年 C++ 算法设计大赛 2025] 金苹果岛
思路
贪心地想,每一次吃金苹果都是翻倍,而红苹果都要吃,那么显然当前缀和最大时翻倍时最优的,因此我们只需要找前缀最大值,记为 ,那么答案就是:
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e5+5;
int a[NA];
signed main(){
int n,m,maxn=1,sum=1;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
sum+=a[i];
maxn=max(maxn,sum);
}
int cul=1;
while(m--) cul*=2;
cout<<sum+maxn*(cul-1);
return 0;
}P10073 [GDKOI2024 普及组] 刷野 II
思路
考虑反向处理。
显然最后的不是 就是 ,所以我们可以双指针来反向推导,显然可以得出,生命值越小的越玩杀,反向推到就是先去掉,这样我们就可以求出最优答案了。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=5e6+5;
int a[NA],res[NA],m;
signed main(){
int d,n;
cin>>d>>n;
int maxn=0,id=0;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int l=1,r=n,ans=0,cnt=n-1;
while(l<=r){
if(a[l]<a[r]){
if(ans-cnt<a[l]){
ans=a[l]+cnt;
}
cnt--;
res[++m]=l;
l++;
}
else{
if(ans-cnt<a[r]){
ans=a[r]+cnt;
}
cnt--;
res[++m]=r;
r--;
}
}
cout<<ans<<"\n";
reverse(res+1,res+m+1);
for(int i=1;i<=m;i++){
cout<<res[i]<<" \n"[i==m];
}
return 0;
}P14971 『GTOI - 2B』DDoS
思路
显然我们希望区间越少越好,所以先可以合并那些本来就互相包含的,和成一些不互相包含的大区间,然后对于大区间,把距离短的先合并,能合并多少是多少,可以用优先队列。
合并小区间就是按 从小到大排序, 从大到小排序合并即可。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e6+5;
int n,m;
struct node{
int l,r;
void read(){
cin>>l>>r;
}
}a[NA];
bool cmp(node x,node y){
if(x.l!=y.l) return x.l<y.l;
return x.r>y.r;
}
vector<pair<int,int> > v;
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
a[i].read();
}
sort(a+1,a+n+1,cmp);
int L=a[1].l,R=a[1].r,cnt=0,ans=0;
for(int i=2;i<=n;i++){
if(a[i].l<=R){
R=max(R,a[i].r);
}
else{
v.push_back({L,R});
cnt+=R-L+1;
ans++;
L=a[i].l,R=a[i].r;
}
}
v.push_back({L,R});
cnt+=R-L+1;
ans++;
if(cnt>m){
cout<<-1;
return 0;
}
int last=v[0].second;
priority_queue<int,vector<int>,greater<int> > q;
for(int i=1;i<v.size();i++){
q.push((v[i].first-1)-(last+1)+1);
last=v[i].second;
}
while(!q.empty()&&cnt+q.top()<=m){
cnt+=q.top();
q.pop();
ans--;
}
cout<<ans;
return 0;
}P15009 [UOI 2019 II Stage] 随从
思路
贪心好题!主要是因为我被卡了。
对于 ,考虑两种情况:
- 前 个中有首领,那么答案为 。
- 前 个中没有首领,那么答案为 。
- 显然不存在前 个中没有首领的情况,而且情况一不包含情况二,情况二也不包含情况一。
我们这里的 表示 , 表示 ,具体为什么自己研究,这里只给出提示。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=2e5+5;
int n;
struct node{
int a,b;
void read(){
cin>>a>>b;
}
}a[NA];
bool cmp(node x,node y){
return x.b<y.b;
}
int pre[NA],suf[NA],sum[NA];
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
a[i].read();
}
sort(a+1,a+n+1,cmp);
pre[0]=1e18;
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i].b;
pre[i]=min(pre[i-1],a[i].a-a[i].b);
}
suf[n+1]=1e18;
for(int i=n;i>=1;i--){
suf[i]=min(suf[i+1],a[i].a);
}
for(int i=1;i<=n;i++){
cout<<min(sum[i]+pre[i],sum[i-1]+suf[i])<<"\n";
}
return 0;
}P15028 [UOI 2021 II Stage] 献给女士的礼物
思路
这是一个很变态的贪心题。
分情况讨论。
设最后得到的数为 :
-
那么我们所有符合条件的数字有 。
-
对于每一个 的二进制表示该位为 ,令 该位为 ,那么剩下来比该位小的必然就是全部为 最优。
对于所有情况取最大值即可。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e6+5;
int a[NA],b[NA];
int ans=0;
signed main(){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
if((a[i]&(~k))==0){
ans+=b[i];
}
}
int w=0;
for(int i=29;i>=0;i--){
if((k>>i)&1){
int X=w|(1<<i);
int sum=0;
for(int j=1;j<=n;j++){
if((a[j]&X)==0){
sum+=b[j];
}
}
ans=max(ans,sum);
}
else{
w|=(1<<i);
}
}
cout<<ans;
return 0;
} P15256 [USACO26JAN2] Purchasing Milk B
思路
我们可以预处理出买 桶牛奶的最低价格,然后开始递归。
对于每种情况考虑买的数量与还需要的数量多一次或少一次,两种情况都要计算,取 即可。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=1e6+5;
int a[NA],b[NA];
int get_ans(int k,int x){
if(x<=0) return 0;
if(k==0) return x*b[0];
int v=1<<k;
if(x<=v){
return min(get_ans(k-1,x),b[k]);
}
else{
int q=x/v,w=x%v;
return min(q*b[k]+get_ans(k-1,w),(q+1)*b[k]);
}
}
signed main(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
b[0]=a[1];
for(int i=1;i<=min(n-1,30ll);i++){
b[i]=min(b[i-1]*2,a[i+1]);
}
for(int i=min(n,31ll);i<=30;i++){
b[i]=2*b[i-1];
}
while(q--){
int x;
cin>>x;
cout<<get_ans(30,x)<<"\n";
}
return 0;
} P15287 「YLLOI-R3-T2」霍元甲
思路
水题。
显然的,只需要知道 个就可以了。 要特判为 。
论证思路:如果 个全是最大的,那么用最小的消耗掉最大的,然后用编号恰好大一位的抵消。
那么剩下一个编号为 的一定大于其他所有的,如果不全是的话就更容易得到结论了。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int n;
cin>>n;
if(n==1) cout<<-1;
else cout<<(n+1)/2;
return 0;
} P15313 [VKOSHP 2025] Magic Ritual
思路
观察题目,不难发现,如果是奇数位那么就可以互相交换且没有代价,偶数位也一样。
所以说我们可以记录原来奇数位上的数和偶数位上的数,然后找到排完序后那些改变了,显然一个奇数变偶数时,一个偶数也会变成奇数,所以答案就是被改变的数字数量除以二。
时间复杂度 。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int NA=2e5+5;
int a[NA];
unordered_map<int,int> mp1,mp2;
signed main(){
int T;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
if(i%2==1) mp1[a[i]]++;
else mp2[a[i]]++;
}
sort(a+1,a+n+1);
int ans=0;
for(int i=1;i<=n;i++){
if(i%2==1){
if(mp1[a[i]]!=0) mp1[a[i]]--;
else ans++,mp2[a[i]]--;
}
else{
if(mp2[a[i]]!=0) mp2[a[i]]--;
else ans++,mp1[a[i]]--;
}
}
cout<<ans/2<<"\n";
}
return 0;
}P15338 [GCPC 2025] Karlsruhe Skyline
思路
code
P15384 构造树的题 / tree
思路
code
P15572 [USACO26FEB] Swap to Win B
思路
code
P15602 [ICPC 2020 Jakarta R] Police Stations
思路
code
P9228 原神
思路
code
P9251 [PA 2022] Palindrom
思路
code
P9422 [蓝桥杯 2023 国 B] 合并数列
思路
code
P9467 [EGOI 2023] Carnival General / 狂欢节总管
思路
code
P9540 「AWOI Round 2 C」数组操作?数组操作!
思路
code
P9562 [SDCPC 2023] Matching
思路
code
P9812 [CCC 2015 S3] Gates
思路
code
P9952 [USACO20OPEN] Social Distancing I B
思路
code
B4218 [常州市赛 2023] ABC 字符串
思路
青题以下众生平等哈。(况且这道题不应该是橙题难度)
code
P10135 [USACO24JAN] Potion Farming S
思路
code
P1016 [NOIP 1999 普及组/提高组] 旅行家的预算
思路
code
P10335 [UESTCPC 2024] 取数游戏
思路
code
P10341 [THUSC 2019] 紧急决策
思路
code
P10464 Task
思路
code
P10505 Dropping Test
思路
code
P1056 [NOIP 2008 普及组] 排座椅
思路
code
P10934 西瓜种植
思路
code
P1095 [NOIP 2007 普及组] 守望者的逃离
思路
code
P1158 [NOIP 2010 普及组] 导弹拦截
思路
code
P1167 刷题
思路
code
P1180 驾车旅游
思路
code
P1182 数列分段 Section II
思路
code
P12633 [UOI 2020] Skyscraper
思路
code
P12687 [KOI 2022 Round 1] 鹅卵石
思路
code
P12688 [KOI 2022 Round 1] 避开
思路
code
P12712 [KOI 2021 Round 1] 超矩形
思路
code
P13032 [GCJ 2021 #1C] Closest Pick
思路
code
P13050 [GCJ 2020 Qualification] Parenting Partnering Returns
思路
code
P13158 [GCJ 2017 Qualification] Oversized Pancake Flipper
思路
code
P13159 [GCJ 2017 Qualification] Tidy Numbers
思路
code
P13244 [GCJ 2014 Qualification] Cookie Clicker Alpha
思路
code
P13312 [GCJ 2012 Qualification] Dancing With the Googlers
思路
code
P13455 [GCJ 2008 Qualification] Train Timetable
思路
code
P1658 购物
思路
code
P1842 [USACO05NOV] 奶牛玩杂技
思路
code
P2920 [USACO08NOV] Time Management S
思路
code
P3078 [USACO13MAR] Poker Hands S
思路
code
P3536 [POI 2012] BON-Vouchers
思路
code
P3743 小鸟的设备
思路
code
P4144 大河的序列
思路
code
P10911 [蓝桥杯 2024 国 B] 数位翻转
思路
code
P10922 Happybob’s Numbers (UBC001B)
思路
code
P11205 「Cfz Round 9」Hope
思路
code
P11247 [GESP202409 六级] 算法学习
思路
code
P11273 「Diligent-OI R1 C」DlgtRank
思路
code
P11349 [NOISG 2024 Finals] Problem Setter
思路
code
P11376 [GESP202412 六级] 运送物资
思路
code
P11960 [GESP202503 五级] 平均分配
思路
code
P1199 [NOIP 2010 普及组] 三国游戏
思路
code
P12802 [AMPPZ 2019] Assimilation
思路
code
P13186 [GCJ 2016 Qualification] Revenge of the Pancakes
思路
code
P1324 矩形分割
思路
code
P5119 [USACO18DEC] Convention S
思路
code
P5199 [USACO19JAN] Mountain View S
思路
code
P5541 [USACO19FEB] Sleepy Cow Herding S
思路
code
P5804 [SEERC 2019] Absolute Game
思路
code
P5961 [BalticOI 2006] coin collector钱币收藏家
思路
code
P5963 [BalticOI 2005] Card 卡牌游戏 (Day0)
思路
code
P6317 [COCI 2006/2007 #3] TENKICI
思路
code
P6446 [COCI 2010/2011 #1] TABOVI
思路
code
P6549 [COCI 2010/2011 #2] KNJIGE
思路
code
P11396 排队
思路
code
P11452 [USACO24DEC] Cake Game S
思路
code
P11540 [Code+#5] 逻辑树
思路
code
P11638 Max,Mex
思路
code
P11854 [CSP-J 2022 山东] 宴会
思路
code
P11868 [威海市赛2024] 抽卡
思路
code
P11931 [CrCPC 2024] AI 的末日
思路
code
P12161 [蓝桥杯 2025 省 Java B] 研发资源分配
思路
code
P12168 [蓝桥杯 2025 省 C/Python A/Java A] 拼好数
思路
code
P12836 [蓝桥杯 2025 国 B] 翻倍
思路
code
P12881 [蓝桥杯 2025 国 C] 宗门大比
思路
code
P13053 [GCJ 2020 #1A] Pattern Matching
思路
code
P13145 [GCJ 2018 #2] Falling Balls
思路
code
P13149 [GCJ 2018 #3] Field Trip
思路
code
P1336 最佳课题选择
思路
code
P1889 [CEOI 1998] 士兵站队
思路
code
P1986 元旦晚会
思路
code
P2434 [SDOI2005] 区间
思路
code
编写不易,给个赞再走呗。
2.0 不知道什么时候会出。