URAL - 1964 Chinese Dialects (数学集合)

1964. Chinese Dialects

Time limit: 1.0 second
Memory limit: 64 MB
Vova moved from Guangzhou to Shenzhen. He immediately found out that the local people don't understand his Cantonese phrases as well. Vova tried chatting with them in Mandarin, but to no success.
Then Vova decided to learn more about Chinese dialects. It turned out that people in China speak  kdifferent dialects, at that at least  a 1 people speak the first dialect, at least  a 2 people speak the second dialect, …, at least  a k people speak the  k-th dialect. How many people speak all  k dialects if the population of China is  n people?

Input

The first line contains integers  n and  k (2 ≤  k ≤ 20; 1 ≤  n ≤ 10 9). The second line contains space-separated integers  a 1, …,  a k (1 ≤  a k ≤  n).

Output

Print the minimum number of people in China that speak all  k dialects of the Chinese language.

Samples

input output
1000000000 2
800000000 800000000
600000000
1000000000 2
500000000 500000000
0

题意:一共n个中国人,有k种方言,有a1个人会说第一种,a2个人会说第二种,ak个人会说第k种。问K种方言全会的人有多少。

解决:算是集合取交集了。将两个方言计算一次,然后将结果看做一种语言往下求结果。

细节:max函数, 每次两个方言都必须有交集,而且和都必须大于n才行。因为结果是取最小。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define fin freopen("in.txt", "r", stdin)
#define line printf("-----------------\n")
using namespace std;
const int maxn = 1000000;

int n, k, a, ans;

int main(){
    cin >> n >> k;
    ans = n;
    for(int i = 1; i <= k; i++){
        cin >> a;
        ans = max(ans + a - n,0);
    }

    cout << ans << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值