2 条题解

  • 2
    @ 2024-4-23 18:14:01
    #include <bits/stdc++.h>
    using namespace std;
    #define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
    #define print(x,y) write(x),putchar(y) 
    template <class T> inline T read(const T sample) {
        T x=0; int f=1; char s;
        while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
        while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
        return x*f;
    }
    template <class T> inline void write(const T x) {
        if(x<0) return (void) (putchar('-'),write(-x));
        if(x>9) write(x/10);
        putchar(x%10^48);
    }
    typedef long long ll;
    typedef pair <ll,ll> Pair;
    const int maxn=1e5+5;
    int n;
    Pair s[maxn];
    int main() {
    	n=read(9);
    	rep(i,1,n) s[i].second=read(9ll),s[i].first=read(9ll);
    	sort(s+1,s+n+1);
    	ll ans=0,sum=0; int l=1,r=n;
    	while(l<r) {
    		if(sum<s[l].first) {
    			if(s[r].second>s[l].first-sum) {
    				s[r].second-=s[l].first-sum;
    				ans+=(s[l].first-sum)*2,sum=s[l].first;
    			}
    			else {
    				sum+=s[r].second;
    				ans+=s[r].second*2; s[r].second=0,--r;
    			}
    			if(sum>=s[l].first) {
    				sum+=s[l].second,ans+=s[l].second,++l;
    			}
    		}
    		else sum+=s[l].second,ans+=s[l].second,++l;
    	}
    	if(l==r) {
    		if(sum>=s[l].first) ans+=s[l].second;
    		else {
    			if(s[l].second>s[l].first-sum)
    				ans+=(s[l].first-sum)*2+s[l].second-s[l].first+sum;
    			else ans+=s[l].second*2;
    		}
    	}
    	print(ans,'\n');
    	return 0;
    }
    
    • 1
      @ 2022-7-14 15:17:16

      贪心

      按照bb从小打大排序,买bb较小的时候能打折全部买就直接打折买,无法打折再看看买了后面bb比较大的能不能让部分小的bb对应的物品打折。

      jfKoi4.png

      • 1

      信息

      ID
      1906
      时间
      1000ms
      内存
      256MiB
      难度
      2
      标签
      递交数
      86
      已通过
      56
      上传者