AtCoder题解 —— AtCoder Beginner Contest 187 —— D - Choose Me —— 贪心

题目相关

题目链接

AtCoder Beginner Contest 187 D 题,https://atcoder.jp/contests/abc187/tasks/abc187_d

Problem Statement

AtCoder City will hold a mayoral election. The candidates are Aoki and Takahashi.
The city consists of N N N towns, the i i i-th of which has A i A_i Ai pro-Aoki voters and B i B_i Bi pro-Takahashi voters. There are no other voters.
Takahashi can make a speech in each town.
If he makes a speech in some town, all voters in that town, pro-Takahashi or pro-Aoki, will vote for Takahashi.
On the other hand, if he does not make a speech in some town, the pro-Aoki voters in that town will vote for Aoki, and the pro-Takahashi voters will not vote.
To get more votes than Aoki, in how many towns does Takahashi need to make speeches at least?

Input

Input is given from Standard Input in the following format:

N
Ai Bi
.
.
AN BN

Output

Print the answer.

Sample 1

Sample Input 1

4
2 1
2 2
5 1
1 3

Sample Output 1

1

Explaination

After making a speech in the third town, Aoki and Takahashi will get 5 and 6 votes, respectively.

Sample 2

Sample Input 2

5
2 1
2 1
2 1
2 1
2 1

Sample Output 2

3

Explaination

After making speeches in three towns, Aoki and Takahashi will get 4 and 9 votes, respectively.

Sample 3

Sample Input 3

1
273 691

Sample Output 3

1

Constraints

  • All values in input are integers.
  • 1 ≤ N ≤ 2 × 1 0 5 1≤N≤2×10^5 1N2×105
  • 1 ≤ A i , B i ≤ 1 0 9 1≤A_i,B_i≤10^9 1Ai,Bi109

题解报告

题目翻译

AtCoder 城市将矩形一次投票,只有两个候选人 Aoki 和 Takahashi。
城市包括 N N N 个城镇,第 i i i 个城镇有 A i A_i Ai 票给 Aoki, B i B_i Bi 票给 Takahashi。
Takahashi 可以在每个城镇发表一次演说。演说后,这个城镇都将所有的票投给 Takahashi。而不在某个城镇演说,该城镇的 B i B_i Bi 票不会参加投票, A i A_i Ai 票继续投给 Aoki。
为了赢得选举,Takahashi 最少要在几个城镇发表演说。

题目分析

根据题意,第 i i i 个城镇有 A i A_i Ai 票给 Aoki, B i B_i Bi 票给 Takahashi。只要 Takahashi 在第 i i i 城镇发表演说,在这个城镇将得到 A i + B i A_i+B_i Ai+Bi 的票,同时 Aoki 将减少 A i A_i Ai 的票。也就是说,Takahashi 在第 i i i 城镇发表演说,他的收益将是 2 ∗ A i + B i 2*A_i+B_i 2Ai+Bi
因此我们只需要记录 Aoki 总票,然后利用贪心的思路,解决本题。

数据分析

样例数据 1

根据样例数据,我们可以得到以下的数据。
Aoki 的总票数为 10 10 10 票。对于 Takahashi 的而言,每个城镇发表演说的收益如下表。

城镇 收益
1 5
2 6
3 11
4 5

使用贪心的思路,我们将这个收益从大到小排序。得到表格如下。

城镇 收益
3 11
2 6
1 5
4 5

在每个城镇发表演说后,Aoki 的票变为 10 − b e n e f i t [ i ] 10-benefit[i] 10benefit[i],只要 Aoki 的票第一次达到负数,说明我们一直找到答案。
我们遍历所有城镇,看 Takahashi 发表几次演说。
Takahashi 先在第 3 个城镇发表演说,这样,Aoki 的票数将变为 10 − 11 = − 1 10-11=-1 1011=1,这样,我们只需要发表一次演说即可。

样例数据 2

根据样例数据,我们可以得到以下的数据。
Aoki 的总票数为 10 10 10 票。对于 Takahashi 的而言,每个城镇发表演说的收益如下表。

城镇 收益
1 5
2 5
3 5
4 5

使用贪心的思路,我们将这个收益从大到小排序。得到表格如下。

城镇 收益
1 5
2 5
3 5
4 5

我们遍历所有城镇,看 Takahashi 发表几次演说。
Takahashi 在第 1 个城镇发表演说,这样,Aoki 的票数将变为 10 − 5 = 5 10-5=5 105=5
Takahashi 在第 2 个城镇发表演说,这样,Aoki 的票数将变为 5 − 5 = 0 5-5=0 55=0
Takahashi 在第 3 个城镇发表演说,这样,Aoki 的票数将变为 0 − 5 = − 5 0-5=-5 05=5
这样,Takahashi 需要发表 3 3 3 次演说。

数据规模分析

N N N 的最大值为 2 ∗ 1 0 5 2*10^5 2105,每个城镇的最大值为 1 0 9 10^9 109,因此 Aoki 的最大票数为 2 ∗ 1 0 5 ∗ 1 0 9 = 2 ∗ 1 0 14 2*10^5*10^9=2*10^{14} 2105109=21014。最大的 b e n e f i t benefit benefit 2 ∗ 1 0 9 + 1 0 9 = 3 ∗ 1 0 9 2*10^9+10^9=3*10^9 2109+109=3109。因此我们需要使用 long long 来描述。

AC 代码

//https://atcoder.jp/contests/abc187/tasks/abc187_d
//D - Choose Me
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

typedef long long ll;

const int MAXN=2e5+4;
ll vote[MAXN];

int main() {
    
    
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    int n;
    cin>>n;
    ll avote=0;
    for (int i=1; i<=n; i++) {
    
    
        ll a,b;
        cin>>a>>b;
        avote += -a;//记录a的票数
        vote[i] = 2*a+b;
    }

    sort(vote+1, vote+n+1, greater<ll>());

    //遍历求解
    int ans=0;
    for (int i=1; i<=n; i++) {
    
    
        avote += vote[i];
        if (avote>0) {
    
    
            ans=i;
            break;
        }
    }
    cout<<ans<<"\n";

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

在这里插入图片描述

时间复杂度

O ( N l o g N ) O(NlogN) O(NlogN)

空间复杂度

O ( N ) O(N) O(N)

猜你喜欢

转载自blog.csdn.net/justidle/article/details/112154845