POJ-2370
来源:互联网 发布:cf抽奖算法 编辑:程序博客网 时间:2024/06/05 06:27
题目大意是关于投票,已知k个组,这k个组中只要有一半以上通过了,就算通过了所以取k/2+1;要想去最少的通过人数,就想办法使得这k/2+1这些组的人数都是最少的,这时可以进行排序,然后取前k/2+1个组;每个组中只要有一半以上的人通过了,就算通过了,所以只要这些k/2+1组的每组超过一半的人通过了,就通过了;及a/2+1,a为每组的人数
#include "stdio.h"#include "stdlib.h"#include <algorithm>using namespace std;int a[102];int main(){int i,n;int sum=0;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",a+i);sort(a,a+n);for(i=0;i<n/2+1;i++)sum+=a[i]/2+1;printf("%d",sum);system("pause");return 0;}
注:
1. 首先用到了c++的排序函数,所以要包含algorithm头文件,并且必须申明命名空间即using namespace std
sort()函数是C++中的排序函数其头文件为:#include<algorithm>头文件;
qsort()是C中的排序函数,其头文件为:#include<stdlib.h>
头文件:
#include <algorithm>
using namespace std;
1.默认的sort函数是按升序排。对应于1)
sort(a,a+n); //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
例如:
int cmp( const int &a, const int &b ){
if( a > b )
return 1;
else
return 0;
}
sort(a,a+n,cmp);
是对数组a降序排序
又如:
int cmp( const POINT &a, const POINT &b ){
if( a.x < b.x )
return 1;
else
if( a.x == b.x ){
if( a.y < b.y )
return 1;
else
return 0;
}
else
return 0;
}
sort(a,a+n,cmp);
是先按x升序排序,若x值相等则按y升序排
- POJ-2370
- POJ 2370选举通过
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 归并排序
- InputFormat的数据划分、Split调度、数据读取三个问题的浅析
- Windows下配置Eclipse for C++
- 2012年湖南省第八届大学生计算机程序设计竞赛 F 题 Kingdoms
- 新手学习Linux下的c++编程
- POJ-2370
- html的<script>脚本:JavaScript入门
- mysql 导入大的sql文件
- [cocos2dx笔记004] android增加静态库工程
- java + selenium测试框架(之截屏) 版本演化四
- 终端之1_对终端进行读写
- 忘记 Linux虚拟机的密码
- 进程和线程
- CSS样式导入的三个方法