B. Ternary Sequence(思维+贪心)Codeforces Round #665 (Div. 2)
原题链接: https://codeforces.com/contest/1401/problem/B
测试样例:
input
3
2 3 2
3 3 1
4 0 1
2 3 0
0 0 1
0 0 1
output
4
2
0
样例解释:
In the first sample, one of the optimal solutions is:
a={2,0,1,1,0,2,1}
b={1,0,1,0,2,1,0}
c={2,0,0,0,0,2,0}
In the second sample, one of the optimal solutions is:
a={0,2,0,0,0}
b={1,1,0,1,0}
c={0,2,0,0,0}
In the third sample, the only possible solution is:
a={2}
b={2}
c={0}
题意: 给定a序列和b序列,通过给定的合成规则使得合成的c序列元素之和最大。
解题思路: 这是一道典型的贪心题,我们想要让总和最大,那么我们就要让a序列中的所有2和b序列中的所有1配对,使构造数字最大。我们统计这个构造后的元素之和。当然,这并未结束,除了这个之后都不能利用配对获得c序列总和增加了,而我们想要让c序列元素之和最大,我们就要去把b序列中的2给删掉,删掉这个之后序列元素之和才不会减少,我们用a序列中剩余的2和0来消掉,之后再判断是否有剩余,有就只能和a序列中的1配对合成,最后造成我们无法避免的减少。经过这些判定,最后我们 统计的总和即是这个问题的解。
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++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t,a[3],b[3],ans;
void solve(){
ans=0;
if(a[2]>=b[1]){
ans+=b[1]*2;
a[2]-=b[1];
b[1]=0;
}
else{
ans+=a[2]*2;
b[1]-=a[2];
a[2]=0;
}
if(b[2]>=a[0]){
b[2]-=a[0];
a[0]=0;
}
else{
a[0]-=b[2];
b[2]=0;
}
if(a[2]>=b[2]){
a[2]-=b[2];
b[2]=0;
}
else{
b[2]-=a[2];
a[2]=0;
}
if(b[2]>0){
if(b[2]>=a[1]){
ans-=a[1]*2;
b[2]-=a[1];
a[1]=0;
}
else{
ans-=b[2]*2;
a[1]-=b[2];
b[2]=0;
}
}
cout<<ans<<endl;
}
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
rep(i,0,2)cin>>a[i];
rep(i,0,2)cin>>b[i];
solve();
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)