甜甜圈合并 贪心排序 codechef Chain of Doughnuts
2015-09-21 11:33
423 查看
https://www.codechef.com/problems/DONUTS
问题描述:
大厨收到了一个紧急订单,要他做一个 N 个甜甜圈构成的链。他注意到厨房里恰好有 N个做好了的甜甜圈,它们中的某些已经连成了链。他需要做的只是将他们连接成一条链。他可以(在某条链中任意一个位置)把一个甜甜圈切开成两半,然后用这个切成两半的甜甜圈来将两条链链接起来。请你帮大厨算出他最少需要切多少个甜甜圈才能完成这个订单。
Given chains of doughnuts of different lengths, we need to join them to form a single change. Two chains can be joined by cutting a doughnut into two and using it to clip
the chains together. We need to minimize the number of cuts needed to form a single chain consisting of all the N doughnuts.
问题描述:
大厨收到了一个紧急订单,要他做一个 N 个甜甜圈构成的链。他注意到厨房里恰好有 N个做好了的甜甜圈,它们中的某些已经连成了链。他需要做的只是将他们连接成一条链。他可以(在某条链中任意一个位置)把一个甜甜圈切开成两半,然后用这个切成两半的甜甜圈来将两条链链接起来。请你帮大厨算出他最少需要切多少个甜甜圈才能完成这个订单。
Given chains of doughnuts of different lengths, we need to join them to form a single change. Two chains can be joined by cutting a doughnut into two and using it to clip
the chains together. We need to minimize the number of cuts needed to form a single chain consisting of all the N doughnuts.
#include<cstdio> #include<set> #include<algorithm> #include<queue> #include<vector> #include<iostream> using namespace std; int main(){ int T;scanf("%d",&T); while(T--){ int n,m; scanf("%d%d",&n,&m); vector<int>qu; int not1=0; for(int i=1;i<=m;++i){ int t;scanf("%d",&t); qu.push_back(t); if(t!=1)not1=1; } if(not1==0){ cout<<n/2<<endl; continue; } if(m==1){ cout<<0<<endl; continue; } sort(qu.begin(),qu.end()); int num=0; int ptr=0; while(true){ int tmp=qu[ptr]; if(tmp==1)++num,++ptr; else{ ++num; --qu[ptr]; } if(num>=qu.size()-ptr-1) break; } cout<<num<<endl; } return 0; }
相关文章推荐
- MAC 使用Jetbrains's产品
- STL之pair对象
- wss2012重装系统后恢复raid信息及导入原有虚拟磁盘
- 如何给Main方法传参数
- EAintroPage的使用
- rails 时间格式化
- 人类是否能够接受人工智能(AI)的存在?
- does not contain bitcode
- Xcode 添加 SVN 出现 nsurlerrordomain error 1012
- mail 发邮件
- 地址待验证----原创 【系统篇】PC 黑苹果破解版OS X Mountain Lion 10.8.X 系统大集合
- Sendmail+dovecot+saslauth+rainloop
- LeetCode Container With Most Water
- AIX nohup 不输出日志
- Failed to install xxxx.apk on device 'emulator-5554!
- 管道的应用(pipe)《深入分析Linux内核源码》 http://blog.csdn.net/wangpengqi/article/details/7996182
- hdu 1302 The Snail
- [LeetCode] Contains Duplicate
- tail -f log.txt命令的小技巧------观察日志log.txt的动态变化
- leetcode 217:Contains Duplicate