Codeforces Round #467 (Div. 2)T1题解

原题链接
http://codeforces.com/contest/937/problem/A
题目大意
给定N个人,和这N个人分数,问有几种分配奖牌的方法,奖牌分配 遵循以下规则
1.至少有一个人获得奖牌
2.得到0分的人无论如何也不能得到奖牌
3.如果得分为X的人得到了奖牌,那么得分>=X的人也必须获得奖牌

题面
A. Olympiad
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The recent All-Berland Olympiad in Informatics featured n participants with each scoring a certain amount of points.

As the head of the programming committee, you are to determine the set of participants to be awarded with diplomas with respect to the following criteria:

At least one participant should get a diploma.
None of those with score equal to zero should get awarded.
When someone is awarded, all participants with score not less than his score should also be awarded.
Determine the number of ways to choose a subset of participants that will receive the diplomas.

Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of participants.

The next line contains a sequence of n integers a1, a2, …, an (0 ≤ ai ≤ 600) — participants’ scores.

It’s guaranteed that at least one participant has non-zero score.

Output
Print a single integer — the desired number of ways.

Examples
inputCopy
4
1 3 3 2
output
3
inputCopy
3
1 1 1
output
1
inputCopy
4
42 0 0 42
output
1
Note
There are three ways to choose a subset in sample case one.

Only participants with 3 points will get diplomas.
Participants with 2 or 3 points will get diplomas.
Everyone will get a diploma!
The only option in sample case two is to award everyone.

Note that in sample case three participants with zero scores cannot get anything.

赛时思路:既然如果得分为X的人得到了奖牌,那么得分>=X的人也必须获得奖牌
,所以我们将序列进行排序并去重,得到大于零的数的个数就可以了。代码使用桶排序。

代码

#include<bits/stdc++.h>
using namespace std;
bool mark[666];//mark数组用来记录一个数字有无出现过
int main() {
    int n,x,sum=0;//n表示数字个数,x为临时变量,sum为分配方案种数
    cin>>n;//输入人数
    for(int i=0; i<n; i++) {
        cin>>x;//输入临时变量
        mark[x]=1;//标记x为出现过
    }
    for(int i=1;i<=600;i++)//遍历mark数组
    if(mark[i])sum++;//如果出现过,种数++
    cout<<sum<<endl;//输出结果
    return 0;//程序要休息喽
}

赛后思考:如果这道题的数据范围更大,不是600,而是6000,60000,甚至10^9,那该怎么办?事实上,C++的STL为我们提供了一个好东西:Set
Set中的数具有以下特性:唯一性,有序性
去重排序再好不过了

代码

#include<bits/stdc++.h>
using namespace std;
int n;//数字个数
set<int>num;/定义集合num存储int类型
int main()
{
    ios::sync_with_stdio(false);//关闭输入同步,据说可以增加程序效率
    cin>>n;//输入数字个数
    for(int i=0;i<n;i++)
    {
        int a;//临时变量
        cin>>a;//输入临时变量
        num.insert(a);//将临时变量放入集合
    }
    if(*num.begin())cout<<num.size()<<endl;//因集合会自动排序,所以利用迭代器侦查num的第一个数是否为0,如果为0输出集合的大小-1,否则输出集合的大小
    else cout<<num.size()-1<<endl;
    return 0;//程序又要休息了
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值