E1.Erase and Extend (Easy Version)
首先我们来证一个东西就是
最优解一定由先删若干次 然后一直copy而来
而不会在中途再删一次
因为在中途再删一次就证明这个后缀不如前缀
那我们不如早开始 就直接删除这个后缀 这样的解肯定是更优的
证明完之后我们直接n2暴力即可
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 1e9+7;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
pair<int,int>p[N];
void solve() {
int n;cin>>n;
int sum=0;
for(int i=1;i<=n;i++){
cin>>p[i].second>>p[i].first;
sum+=p[i].second;
}
sort(p+1,p+n+1);
int now=-1,ans=0;
for(int i=1;i<=n;i++){
if(p[i].first>=sum)break;
if(now<=p[i].first){
now=p[i].first;
now+=p[i].second;
ans+=p[i].second;
if(now>sum){
ans-=now-sum;
break;
}
}else{
now+=p[i].second;
ans+=p[i].second;
if(now>sum){
ans-=now-sum;
break;
}
}
}
cout<<sum*2-ans<<endl;
}
signed main(){
fast
int t;t=1;//cin>>t;
while(t--) {
solve();
}
return ~~(0^_^0);
}
标签:const,int,sum,Codeforces,726,second,ans,now,E1
From: https://www.cnblogs.com/ycllz/p/16800929.html