[NOIP2017模拟]序列操作
2017-11-08 08:26
344 查看
2017.11.6 T2 2038
样例数据1
输入
3 5
1 2 5
1 2 3 2 1
输出
3
样例数据2
输入
5 5
1 3 3 4 5
1 2 4 4 4
输出
5
分析:看到都知道应该贪心每次选最大的去-1,就是看实现了(O(N2)的随随便便都应该能先想到保底对吧)。对于考场上就是暴力还写炸的我,还是讲讲正解吧……
1、用桶乱搞,开一个桶数组记录每一种数值有多少个,每次就从最大的开始找到c个并把这些被减数值的个数移到数值-1的地方去,由于数值一共就10000种,所以每组数据c再大也没法让我移动超过10000次,再加上本身n就没c大,也限制了c,胡乱想想发现确实不好卡这种算法。
2、二分+贪心,二分能分的次数,记录每个数需要被减几次(用贪心,每次都从最大的开始,c=1的时候就是最大数-1,c=2的时候就是最大数和次大数-1,反正尽量让大数去被减,某个数的数值不够减就往后移),最后比较够不够减,所以就根据能否实现二分查找(貌似用语言说不清楚orz,看代码吧,虽然代码也不清楚orzorz)。
3、线段树,还是很暴力的实现了,感觉和乱搞差不多,只是有一些标记优化。
代码
乱搞玄学
二分标算,真的说不清楚,意会!意会!
本题结。
样例数据1
输入
3 5
1 2 5
1 2 3 2 1
输出
3
样例数据2
输入
5 5
1 3 3 4 5
1 2 4 4 4
输出
5
分析:看到都知道应该贪心每次选最大的去-1,就是看实现了(O(N2)的随随便便都应该能先想到保底对吧)。对于考场上就是暴力还写炸的我,还是讲讲正解吧……
1、用桶乱搞,开一个桶数组记录每一种数值有多少个,每次就从最大的开始找到c个并把这些被减数值的个数移到数值-1的地方去,由于数值一共就10000种,所以每组数据c再大也没法让我移动超过10000次,再加上本身n就没c大,也限制了c,胡乱想想发现确实不好卡这种算法。
2、二分+贪心,二分能分的次数,记录每个数需要被减几次(用贪心,每次都从最大的开始,c=1的时候就是最大数-1,c=2的时候就是最大数和次大数-1,反正尽量让大数去被减,某个数的数值不够减就往后移),最后比较够不够减,所以就根据能否实现二分查找(貌似用语言说不清楚orz,看代码吧,虽然代码也不清楚orzorz)。
3、线段树,还是很暴力的实现了,感觉和乱搞差不多,只是有一些标记优化。
代码
乱搞玄学
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<ctime> #include<cmath> #include<algorithm> #include<cctype> #include<iomanip> #include<queue> #include<set> using namespace std; int getint() { int sum=0,f=1; char ch; for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar()); if(ch=='-') { f=-1; ch=getchar(); } for(;isdigit(ch);ch=getchar()) sum=(sum<<3)+(sum<<1)+ch-48; return sum*f; } const int maxn=1000010; int n,m,c,maxh,tot; int h[maxn],cnt[10010]; bool bj; int main() { freopen("sequence.in","r",stdin); freopen("sequence.out","w",stdout); n=getint(),m=getint(); for(int i=1;i<=n;++i) { h[i]=getint(); if(h[i]>0) { cnt[h[i]]++; maxh=max(maxh,h[i]); } } for(int i=1;i<=m;++i) { c=getint(); tot=0,bj=false; for(int j=maxh;j>=1;--j) { tot+=cnt[j]; if(tot>=c) { bj=true; cnt[j-1]+=cnt[j]-(tot-c); cnt[j]=tot-c+cnt[j+1]; for(int k=j+1;k<=maxh;++k) cnt[k]=cnt[k+1]; if(j!=maxh||(tot==c&&j==maxh)) { cnt[maxh]=0; maxh=maxh-1; } break; } } if(bj==false) { cout<<i-1<<'\n'; return 0; } } cout<<m<<'\n'; return 0; }
二分标算,真的说不清楚,意会!意会!
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<ctime> #include<cmath> #include<algorithm> #include<cctype> #include<iomanip> #include<queue> #include<set> using namespace std; int getint() { int sum=0,f=1; char ch; for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar()); if(ch=='-') { f=-1; ch=getchar(); } for(;isdigit(ch);ch=getchar()) sum=(sum<<3)+(sum<<1)+ch-48; return sum*f; } const int maxn=1000010; int h[maxn],c[maxn]; int n,m; int cnt[maxn]; bool comp(const int &a,const int &b) { return a>b; } bool check(int k) { for(int i=0;i<=n+1;++i) cnt[i]=0; long long sum=0; for(int i=1;i<=k;++i) { if(c[i]>n)//如果有一次操作需要-1的个数比n还大,显然就不行 return false; cnt[c[i]]++;//记录每种c值的数量(方便记录前缀和) sum=sum+c[i];//记录需要减去的总和 } for(int i=n;i>=1;--i) cnt[i]+=cnt[i+1];//记录前缀和(相当于计算每个数需要被减几次,比如最大数就需要减cnt[1]次,体现贪心的思想,让尽量多的-1由大数承担,也就只有大数能-1更多次嘛) long long val=0; for(int i=1;i<=n;++i) { val=val+min(h[i],cnt[i]);//记录能够减的数值的总和 if(h[i]<cnt[i]) cnt[i+1]+=cnt[i]-h[i];//不够就要把剩下的移到下一个数需要减的次数里 } return val==sum;//最终判断移动完到底能不能减去sum个1 } int main() { //freopen("sequence.in","r",stdin); //freopen("sequence.out","w",stdout); n=getint(),m=getint(); for(int i=1;i<=n;++i) h[i]=getint(); sort(h+1,h+n+1,comp); for(int i=1;i<=m;++i) c[i]=getint(); int l=0,r=m,ans=0; while(l<=r)//二分能够进行多少次操作 { int mid=l+r>>1; if(check(mid)) ans=mid,l=mid+1; else r=mid-1; } cout<<ans<<'\n'; return 0; }
本题结。
相关文章推荐
- [NOIP2017模拟]操作
- 【JZOJ 5431】【NOIP2017提高A组集训10.28】序列操作
- JZOJ 5431. 【NOIP2017提高A组集训10.28】序列操作
- JZOJ5431. 【NOIP2017提高A组集训10.28】序列操作
- 【JZOJ5231】【NOIP2017模拟A组模拟8.5】序列问题
- [NOIP模拟] 序列操作
- NOIP模拟 序列操作【线段树】
- [NOIP2017模拟]最佳序列
- 【NOIP模拟】 (11.6) T2 序列操作
- 【jzoj5231】【NOIP2017模拟A组模拟8.5】【序列问题】 【分治】
- 【JZOJ5431】【NOIP2017提高A组集训10.28】序列操作
- jzoj5249 【NOIP2017提高A组模拟8.10】文本编辑器 (序列修改类问题,数据结构)
- 【jzoj5359】【NOIP2017提高A组模拟9.12】【Arrays and Palindrome】
- 【jzoj5286】【NOIP2017提高A组模拟8.16】【花花的森林 】【时间倒流】
- jzoj5398. 【NOIP2017提高A组模拟10.7】Adore
- NOIP2017 模拟考试 day2 2017.10.07
- 【jzoj5288】【NOIP2017提高组A组模拟8.17】【球场大佬】
- jzoj5347. 【NOIP2017提高A组模拟9.5】遥远的金字塔 容斥
- 【jzoj5315】【NOIP2017提高A组模拟8.19】【小串串】【sam 】
- JZOJ 5376. 【NOIP2017提高A组模拟9.19】Candy