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.
#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;
}