A. Array Rearrangment(思维)Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)
原题链接: https://codeforces.com/contest/1445/problem/A
测试样例
input
4
3 4
1 2 3
1 1 2
2 6
1 4
2 5
4 4
1 2 3 4
1 2 3 4
1 5
5
5
output
Yes
Yes
No
No
Note
In the first test case, one can rearrange b so it’ll look like [1,2,1]. In this case, 1+1≤4; 2+2≤4; 3+1≤4.
In the second test case, one can set b to [5,2], then 1+5≤6; 4+2≤6.
In the third test case, no matter how one shuffles array b, a4+b4=4+b4>4.
In the fourth test case, there is only one rearrangement of array b and it doesn’t satisfy the condition since 5+5>5.
题意: 给定两个长度为 n n n的整数序列 a a a和 b b b,你需要重新排列数组 b b b使得 a i + b i ≤ x a_i+b_i \leq x ai+bi≤x。若可以,则输出YES,否则输出NO。
解题思路: 简单的一道思维题,既然数组 b b b可以排列,我们为了满足题目条件,当然是选择数组 a a a的最大元素和数组 b b b中的最小元素配对,即我们可以对数组 a a a从小到大排序,数组 b b b从大到小排序。最后通过判断是否可以即可解决。
AC代码
/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>//POJ不支持
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=a;i>=n;i--)
using namespace std;
const int inf=0x3f3f3f3f;//无穷大。
const int maxn=1e5;//限定值。
typedef long long ll;
int t,n,x;
int a[55],b[55];
int main(){
while(cin>>t){
while(t--){
cin>>n>>x;
rep(i,0,n-1){
cin>>a[i];
}
rep(i,0,n-1){
cin>>b[i];
}
sort(a,a+n);
sort(b,b+n,greater<int>() );
bool flag=false;
rep(i,0,n-1){
if(a[i]+b[i]>x){
flag=true;
break;
}
}
if(flag){
cout<<"NO"<<endl;
}
else{
cout<<"YES"<<endl;
}
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)