原题链接: https://codeforces.com/contest/1443/problem/C
测试样例
input
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2
output
5
3
2
3
题意: 你需要点个不同的菜,你有两种方式可以选择,一种是点外卖,花费
时间,一种是自己拿,花费
时间。你需要在最少的时间中弄完这
个菜。
解题思路: 首先我们要清楚一点, 不管我们点了多少外卖,自己拿了多少菜,这时间消耗永远是(点外卖中的最大值,自己拿的时间总和)。清楚了这一点,那么我们自然可以枚举我们要拿的外卖 (我们选了一个外卖,那个其他点外卖时间小于等于它的都会被选择。) ,那么在枚举之前我们可以用前缀和做一下优化,统计自己拿的时间总和,同样我们也需进行排序,这个自定义排序应保证外卖时间由小到大,而外卖时间相同的话自己拿的时间由大到小。之后就遍历取最优值即可。具体看AC代码。
AC代码
/*
*
*/
//POJ不支持
using namespace std;
const int inf=0x3f3f3f3f;//无穷大。
const int maxn=2e5+6;//限定值。
typedef long long ll;
int t,n;
struct node{
ll a,b;
};
bool cmp(node a1,node a2){
if(a1.a==a2.a){
//相等的话,我肯定要让预算更小的排后面。
return a1.b>a2.b;
}
else{
return a1.a<a2.a;
}
}
node datas[maxn];
ll pre[maxn];
int main(){
while(cin>>t){
while(t--){
cin>>n;
rep(i,1,n){
cin>>datas[i].a;
}
rep(i,1,n){
cin>>datas[i].b;
}
sort(datas+1,datas+n+1,cmp);
pre[n+1]=0;
per(i,n,1){
pre[i]=pre[i+1]+datas[i].b;
}
ll ans=inf;
//注意,我们将不取外卖的点设为data[0],故我们对其进行初始化。
datas[0].a=datas[0].b=0;
rep(i,0,n){
ans=min(ans,max(datas[i].a,pre[i+1]));
}
cout<<ans<<endl;
}
}
return 0;
}