186. The Chain
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard input
output: standard output
output: standard output
Smith has N chains. Each chain is the sequence of successively connected links. The length of each chain is known: the first chain contains L1 links, the second - L2, ..., the last one - LN.
He can make a following series of actions in a minute:
1. to unchain one link
2. to remove or to put into the unchained link some other links of any chain
3. to chain the link
Your task is to determine the minimum time which will take the smith to connect all the chains in one line, i.e. the chain will look like a chain made up of successively connected links.
He can make a following series of actions in a minute:
1. to unchain one link
2. to remove or to put into the unchained link some other links of any chain
3. to chain the link
Your task is to determine the minimum time which will take the smith to connect all the chains in one line, i.e. the chain will look like a chain made up of successively connected links.
Input
The first line contains natural number N<=100. The second line contains L1, L2, ..., LN (1<=Li<=100, for all i = 1..N).
Output
Output the only integer number - the solution to the problem.
Sample test(s)
Input
2
3 4
3 4
Output
1
恶心的描述,英语差真心被秒,后来看了网上大牛的翻译才理解,其实就是一个贪心的过程。
建议大家看看大牛的博客:大牛博客
贴个我的代码:
恶心的描述,英语差真心被秒,后来看了网上大牛的翻译才理解,其实就是一个贪心的过程。
建议大家看看大牛的博客:大牛博客
贴个我的代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)
using namespace std;
int const nMax = 1010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;
// std::ios::sync_with_stdio(false);
int n;
int a[nMax],b,c;
int main(){
scanf("%d",&n);
FOR(i,n) {
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
int i=1;
int l=n;
int ans=0;
//每次都将最小的那个圈拿来拆
while(1){
if(l==1) break;
if(l==2) {
l--;
ans++;
break;
}
l--;
a[i]--;
ans++;
if(a[i]==0) {
l--;
i++;
}
}
printf("%d\n",ans);
return 0;
}