题意
给你两个集合( a , b ),每个集合都有 n 个元素,再给一个 x ,问你能不能通过改变 b 集合的元素顺序使得任意 i ,满足 ai + bi <= x
思路
a 从小到大排序,b 从大到小排序,然后看对应位置 ai + bi 与 x 关系,都满足就Yes , 否则No.
代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e7+10;
const int mod=10007;
int a[110],b[110];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
ios::sync_with_stdio(false);
int t,n,x;
cin>>t;
while(t--)
{
cin>>n>>x;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>b[i];
sort(a+1,a+n+1);
sort(b+1,b+n+1,cmp);
int flag=0;
for(int i=1;i<=n;i++)
{
if(a[i]+b[i]>x)
{
flag=1;
break;
}
}
if(flag) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}
题意
给 a (第一场比赛的 第100名 的得分), b(第一场的 前100名 在第二场的最少得分) , c (第二场比赛的 第100名 的得分), d (第二场的 前100名 在第一场的最少得分),让你求两场总得分的第100名最小可能是多少分
思路
50人第一场a+第二场c
50人第一场a+第二场b
50人第一场d+第二场c
第100名就是第二大的
代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e7+10;
const int mod=10007;
int p[5];
int main()
{
ios::sync_with_stdio(false);
int t,a,b,c,d;
cin>>t;
while(t--)
{
cin>>a>>b>>c>>d;
p[1]=a+c;
p[2]=a+b;
p[3]=c+d;
sort(p+1,p+3+1);
cout<<p[2]<<endl;
}
return 0;
}