Gym
来源:互联网 发布:打印机排版软件 编辑:程序博客网 时间:2024/06/04 19:10
题意:
n个不同颜色,每种颜色有a[i]个 ,问:不同颜色间两两配对,最多能配对多少对?
思路:
设maxn为a[i]的最大值,sum为a[i]的和。
贪心的想,如果maxn*2>sum,那么我们用其他颜色全部去和maxn的颜色配对是最好的。如果maxn*2<=sum,这个时候我们把所有袜子按照颜色排序,比如颜色为1的有两个,颜色为2的有两个,颜色为3的有两个,那么排序后为1 1 2 2 3 3,因为每种颜色不会超过sum/2个,此时我们只要将下标为0的和下标为sum/2的配对,下标为1的和下标为sum/2+1的配对.....即可,所以如果是总共偶数个,可以全部配对,奇数个可以只剩下一个。
代码:
#include <cstdio>#include <algorithm>#include <iostream>#include <vector>#include <cstring>#include <string>#include <cmath>#include <map>#include <queue>#include <bitset>#include <stack>#include <sstream>#define IO ios::sync_with_stdio(false),cin.tie(0);#define pb push_back#define pii pair<int,int>#define mp make_pair#define rep(i,a,b) for(int i=a;i<=b;i++)#define dep(i,a,b) for(int i=a;i>=b;i--)#define mem(a,b) memset(a,b,sizeof(a))#define debug(x) cout<<"["<<x<<"]"<<endl;#define lson id<<1,l,mid#define rson id<<1|1,mid+1,rtypedef long long ll;using namespace std;const double eps=1e-7;const int MOD=1e9+7;const ll INFLL=0x3f3f3f3f3f3f3f3f;const int INF=0x3f3f3f3f;const int MAXN=1e5+5;int main() { ll n,x,maxn=0,sum=0; cin>>n; rep(i,1,n)scanf("%lld",&x),maxn=max(maxn,x),sum+=x; if(maxn*2>sum)printf("%lld\n",sum-maxn); else printf("%lld\n",sum/2);}
阅读全文
0 0
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Linux系统中的EOT和EOF
- WEBAPI querySelector以及queryselectorAll方法详解
- 安卓自定义View----实现TextView可设置drawable宽高度
- JAVA阻塞队列LinkedBlockingQueue 以及非阻塞队列ConcurrentLinkedQueue 的区别
- log4j.properties 详解
- Gym
- 线段树入门(一)
- 前缀式计算
- 数据分析与数据挖掘的区别
- Ubantu安装Vim(VirtualBox虚拟机)
- Nginx学习之十二-负载均衡-加权轮询策略剖析
- 几种去重算法的实现
- csv文件数据导入到hive操作说明
- 词向量源码解析:(5.5)ngram2vec源码解析之pairs2vocab