当前位置:网站首页>B. Ternary Sequence(思维+贪心)Codeforces Round #665 (Div. 2)
B. Ternary Sequence(思维+贪心)Codeforces Round #665 (Div. 2)
2022-06-24 15:36:00 【51CTO】
原题链接: 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代码:
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
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;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
边栏推荐
- 个人常用的高效工具
- Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
- SIGGRAPH 2022 | 真实还原手部肌肉,数字人双手这次有了骨骼、肌肉、皮肤
- 安装ImageMagick7.1库以及php的Imagick扩展
- 几种常见的DoS攻击
- One article explains Jackson configuration information in detail
- Efficient tools commonly used by individuals
- 存在安全隐患 路虎召回部分混动揽运
- leetcode 139. Word Break 单词拆分(中等)
- Summary of common tools and usage
猜你喜欢

Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021

运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电

Jenkins 镜像无法更新插件中心的3种解决方法

Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight

Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières
![[application recommendation] the hands-on experience and model selection suggestions of apifox & apipost in the recent fire](/img/dd/24df91a8a1cf1f1b9ac635abd6863a.png)
[application recommendation] the hands-on experience and model selection suggestions of apifox & apipost in the recent fire

一文理解OpenStack网络

Vim编辑器的最常用的用法

Mongodb introductory practical tutorial: learning summary directory

Nifi from introduction to practice (nanny level tutorial) - environment
随机推荐
Crmeb multi merchant system applet authorization problem solving paste
Build go command line program tool chain
Logging is not as simple as you think
One article explains Jackson configuration information in detail
Introduction to new features of ECMAScript 2019 (ES10)
Some experiences of K project: global template highlights
国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子
60 个神级 VS Code 插件!!
Istio FAQ: failed to resolve after enabling smart DNS
Wi-Fi 7 来啦,它到底有多强?
The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television
Paper: Google TPU
Three solutions for Jenkins image failing to update plug-in Center
Decomposition of Uber dependency injection into dig source code analysis
2021-05-04: given a non negative integer C, you need to judge whether there are two integers a and B, so that a*a+b*b=c.
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
New de debugging
Two problems of qtreewidget returning as DLL in singleton mode
10 hands-free idea plug-ins. These codes do not need to be written (the second bullet)
2021-04-18: given a two-dimensional array matrix, the value in it is either 1 or 0,