【Codeforces】967B Watering System【Greedy】

【Codeforces】967B Watering System

【The main idea of ​​the title】

There are n water outlets, and the water output of the i-th water outlet is s i , add the amount of water of A to the water inlet, and ask how many water outlets you plug at least to make the water output of the first water pipe at least B. Calculation formula of water output s 1 A S , S represents the sum of the water output from the unblocked outlet.

【answer】

The first time I saw this question, I didn't see that the water output was at least B. As a result, I was stumped. I was really worried about my eyes. We started from the big one to ensure the smallest.

【code show as below】

#include<cstdio>
#include<algorithm>
using namespace std;
int n,A,B,a[100005],Sum;
int main(){
//  freopen("B.in","r",stdin);
//  freopen("B.out","w",stdout);
    scanf("%d%d%d",&n,&A,&B);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),Sum+=a[i];
    if(n<=1){printf("0\n");return 0;}
    sort(a+2,a+1+n);
    if(a[1]*A/Sum>=B){printf("0\n");return 0;}
    for(int i=n;i>1;i--){
        Sum-=a[i];
        if(a[1]*A/Sum>=B){printf("%d",n-i+1);break;}
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568895&siteId=291194637