【Codeforces】976E Well played!【Greedy】

【Codeforces】976E Well played!

【The main idea of ​​the title】

gives you n lines, one per line h p i , d m g i , you have two operations:
1. h p i = h p i 2
2. d m g i = h p i
You can perform A times 1 operation, B times 2 operations
Ask your last i = 1 n d m g i maximum value.

【answer】

This greed is hard to imagine, that is to put all A 1 operations in one h p i body, make h p i << A , then find m a x ( h p i << A d m g i ) That's it.
But there is one kind of data that can get stuck:

5 1 18
7 3
7 6
8 6
8 5
1 2

If you just write it like this, then it will be 39, but in fact, you can do 40, and put it all on 3.
So enumerate i and find the maximum value.

【code show as below】

#include<cstdio>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
int n,A,B,t;LL Ans,Sum,Num;
struct xcw{LL hp,dam;bool operator <(const xcw b)const{return (hp-dam)>(b.hp-b.dam);}}a[200005];
LL read(){
    LL ret=0;char ch=getchar();bool f=1;
    for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');
    for(; isdigit(ch);ch=getchar()) ret=ret*10+ch-'0';
    return ret;
}
int main(){
//  freopen("E.in","r",stdin);
//  freopen("E.out","w",stdout);
    n=read(),A=read(),B=read();
    for(int i=1;i<=n;i++) a[i]=(xcw){read(),read()},Num+=a[i].dam;
    if(!B){cout<<Num<<endl;return 0;}
    sort(a+1,a+1+n);
    for (int i=1;i<=B;i++) Sum+=max(0LL,a[i].hp-a[i].dam);
    for (int i=1;i<=B;i++) Ans=max(Ans,Sum-max(0LL,a[i].hp-a[i].dam)-a[i].dam+(a[i].hp<<A));
    for (int i=B+1;i<=n;i++) Ans=max(Ans,Sum-max(0LL,a[B].hp-a[B].dam)-a[i].dam+(a[i].hp<<A));
    cout<<Ans+Num<<endl;
    return 0;
}

Guess you like

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