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升序排

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击