贪心篇
目录:
P1115 最大子段和
P1803 凌乱的yyy / 线段覆盖
P1223 排队接水
CF1003D Coins and Queries
P2168 [NOI2015] 荷马史诗
P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G
P1080 [NOIP2012 提高组] 国王游戏
P1115 最大子段和
直接枚举sum的值即可
//P1115
#include<iostream>
using namespace std;
const int N = 2e6+10;
int n;
int a[N];
int sum;
int main()
{
int maxn = -0x3f3f3f3f;
cin>>n;
for(int i = 1;i <= n;i++)
{
cin>>a[i];
}
for(int i = 1;i <= n;i++)
{
if(sum < 0)sum = 0;
sum+=a[i];
if(sum > maxn)maxn = sum;
}
cout<<maxn<<endl;
return 0;
}
P1803 凌乱的yyy / 线段覆盖
思路:直接枚举右端点即可
//P1803 凌乱的yyy / 线段覆盖
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int n;
int ans,lh;//ans记录答案,lh是上一场比赛的最后时间
struct qujian
{
int l,r;
}a[N];
bool cmp(qujian a,qujian b)
{
return a.end < b.end;//以右端点进行排序
}
int main()
{
cin>>n;
for(int i = 1;i <= n;i++)
{
cin>>a[i].l>>a[i].r;
}
sort(a+1,a+1+n,cmp);
for(int i = 1;i <= n;i++)
{
if(lh <= a[i].start)
{
lh = a[i].end;
ans++;
}
}
cout<<ans<<endl;
return 0;
}
P1223 排队接水
//P1223 然后这个题的策略显然是接水时间少的排前面,所以按l排序
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
double ans;
struct qujian
{
int l,r;
}a[N];
bool cmp(qujian e,qujian b)
{
return e.r < b.r;//把l排序,用r记录了这个点原来的编号
}
int main()
{
cin>>n;
for(int i = 1;i <= n;i++)
{
cin>>a[i].r;
a[i].l = i;
}
sort(a+1,a+1+n,cmp);
for(int i = 1;i <= n;i++)
{
cout<<a[i].l<<" ";
}
cout<<endl;
for(int i = 1;i <= n;i++)
{
ans+=a[i].r*(n-i);
}
printf("%.2f",ans/n);
return 0;
}
CF1003D Coins and Queries
思路:要我们求最小硬币面值,那我们肯定是从大往小找啊!!!(又因为是2的幂次所以注意些,要写成 i /= 2)
PS:在记录的时候一定要记得多组清空啊!!全局变量只清空一次!!!!
咱们也可以不用map记录每个数出现了多少次,也可以直接开一个桶记录一下即可
//CF1003D Coins and Queries
#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;
const int N = 2e5+10;
int a[N];
int n,q,ans;
int main()
{
scanf("%d%d",&n,&q);
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
mp[a[i]]++;
}
while(q--)
{
int w;
scanf("%d",&w);
ans = 0;
for(int i = 1 << 30;i >= 1;i /= 2)
{
int x = min(mp[i],w/i);
ans += x;
w -= i*x;
}
if(w) puts("-1");
else printf("%d\n",ans);
}
return 0;
}
P2168 [NOI2015] 荷马史诗
P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G
P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
//P1090
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
int a[N];
bool flag = false;
int n;
int sum = 0;
int main()
{
cin>>n;
for(int i = 1;i <= n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
while(1)
{
int j = 1;
while(a[j] == 0)j++;
if(j == n)break;//只存在一个堆
else
{
if(a[j] += a[j+1])
sum += a[j];
for(int l = j+1;l < n;l++)
{
a[l] = a[l+1];//将j后的果子向前移动
}
n--;
}
for(int l = j;l < n;l++)
{
if(a[l] > a[l+1])swap(a[l],a[l+1]);
}
}
cout<<sum<<endl;
return 0;
}
P1080 [NOIP2012 提高组] 国王游戏
//P1090
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
int a[N];
bool flag = false;
int n;
int sum = 0;
int main()
{
cin>>n;
for(int i = 1;i <= n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
while(1)
{
int j = 1;
while(a[j] == 0)j++;
if(j == n)break;//只存在一个堆
else
{
if(a[j] += a[j+1])
sum += a[j];
for(int l = j+1;l < n;l++)
{
a[l] = a[l+1];//将j后的果子向前移动,其实这里很清楚就是把前面空的堆删掉啊
}
n--;
}
for(int l = j;l < n;l++)
{
if(a[l] > a[l+1])swap(a[l],a[l+1]);
}
}
cout<<sum<<endl;
return 0;
}