C - Twins(贪心)
Problem description
Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It's hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it's like.
Now let's imagine a typical morning in your family. You haven't woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, ncoins of arbitrary values a1, a2, ..., an. But as Mom was running out of time, she didn't split the coins for you two. So she scribbled a note asking you to split the money equally.
As you woke up, you found Mom's coins and read her note. "But why split the money equally?" — you thought. After all, your twin is sleeping and he won't know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you've decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of coins. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the coins' values. All numbers are separated with spaces.
Output
In the single line print the single number — the minimum needed number of coins.
Examples
Input
2
3 3
Output
2
Input
3
2 1 2
Output
2
Note
In the first sample you will have to take 2 coins (you and your twin have sums equal to 6, 0 correspondingly). If you take 1 coin, you get sums 3, 3. If you take 0 coins, you get sums 0, 6. Those variants do not satisfy you as your sum should be strictly more that your twins' sum.
In the second sample one coin isn't enough for us, too. You can pick coins with values 1, 2 or 2, 2. In any case, the minimum number of coins equals 2.
解题思路:题目的意思就是有n块硬币,要求从中挑选出num个硬币,使得其价值总和nowsum刚好大于剩下硬币的价值总和sum,并且num是最小。怎么解决呢?将币值排序(从小到大),然后从后(币值较大的硬币)往前依次取硬币,只要第一次出现nowsum>sum,此时的num就是要取的最小硬币数,小贪心,水过。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,a[],sum=,num=,nowsum=;
cin>>n;
for(int i=;i<n;++i){cin>>a[i];sum+=a[i];}
sort(a,a+n);
for(int i=n-;i>=;--i){
nowsum+=a[i];sum-=a[i];num++;
if(nowsum>sum)break;
}
cout<<num<<endl;
return ;
}
C - Twins(贪心)的更多相关文章
- Codeforce 140C (贪心+优先队列)补题
C. New Year Snowmen time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
随机推荐
- hadoop job history server
默认情况下是没有启动的,需要配置完后手工启动服务. 1. 修改mapred-site.xml,添加如下内容(cluster mode, RM) <property> <nam ...
- 转:Python结合P有winauto进行windows UI自动化
https://blog.csdn.net/z_johnny/article/details/52778064
- html中的小知识
引用外部样式 样式表,如果是引用外部样式,不需要再写style标签了,因为 <link rel="stylesheet" type="text/css" ...
- iproute2和tc的高级路由用法
#Linux advanced router ip link show #显示链路 ip addr show #显示地址(或ifconfig) ip route show #显示路由(route -n ...
- SSL证书在线申请和取回证书指南
1.客服下单后用户会收到一封邮件,来验证接收证书的邮箱;如图1所示:只有完成此邮箱验证才能正常申请证书; 请注意:为了确保您或您的用户能正常收到WoSign证书管理系统自动发送的邮件,请一定要把系统发 ...
- PAT_A1030#Travel Plan
Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...
- [Ynoi2016]谁的梦
题目大意: 给定$n$个序列,要你从每个序列中选一个非空子串然后拼起来,拼成的序列的贡献为不同元素个数. 支持单点修改,在开始时和每次修改完后,输出所有不同选取方案的贡献和. 解题思路: 窝又来切Yn ...
- 【hihocoder 1295】Eular质数筛法
[题目链接]:http://hihocoder.com/problemset/problem/1295 [题意] [题解] 可以在O(N)的复杂度内求出1..N里面的所有素数; 当然受空间限制,N可能 ...
- 生成PW配置和BFD配置
保存在配置文件中 def Main(): buf = '' f = open('pw.cfg','w') for i in range(2016): pwid = str(102 + i) buf + ...
- 2767 Proving Equivalences 至少加几条边让全部图变成强连通模板题
#include<stdio.h> #include<string.h> #define N 21000 struct node { int u,v,next; }bian[N ...