题意:有n个班级,每个班级有a[i]个人,b[i]杯奶茶
每个人至多喝一杯奶茶,且不能喝自己班的
问能喝到奶茶的最多总人数
n<=1e6,a[i],b[i]<=1e9
思路:
做法一:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
//typedef pair<ll,ll>P;
#define N 1000010
#define M 200010
#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const int MOD=1e9+,inv2=(MOD+)/;
double eps=1e-;
int INF=1e9;
int inf=0x7fffffff;
int dx[]={-,,,};
int dy[]={,,-,}; ll a[N],b[N]; ll read()
{
ll v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void solve()
{
int n;
scanf("%d",&n);
ll s=,ans=;
rep(i,,n)
{
a[i]=read(),b[i]=read();
s+=b[i];
}
rep(i,,n) ans+=min(a[i],s-b[i]);
ans=min(ans,s);
printf("%I64d\n",ans);
} int main()
{
int cas=read();
while(cas--) solve();
return ;
}
做法二:题解做法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
#define N 1100000
#define M 4100000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const ll MOD=1e9+,inv2=(MOD+)/;
double eps=1e-;
int INF=1e9; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} int a[N],b[N]; int main()
{
//freopen("1.in","r",stdin);
int cas;
scanf("%d",&cas);
while(cas--)
{
int n=read();
ll sa=,sb=;
rep(i,,n)
{
a[i]=read(); b[i]=read();
sa+=a[i];
sb+=b[i];
}
if(sa>sb)
{
int flag=,s=;
rep(i,,n)
if(a[i]+b[i]-(sa-sb)>sb)
{
flag=;
s=a[i]+b[i]-(sa-sb)-sb;
}
if(flag) printf("%I64d\n",sb-s);
else printf("%I64d\n",sb);
}
else
{
int flag=,s=;
rep(i,,n)
if(a[i]+b[i]-(sb-sa)>sa)
{
flag=;
s=a[i]+b[i]-(sb-sa)-sa;
}
if(flag) printf("%I64d\n",sa-s);
else printf("%I64d\n",sa);
}
} return ;
}