2021.7.30 Codeforces Round #723 (Div. 2)

2024-02-26 17:58
文章标签 codeforces round div 30 2021.7 723

本文主要是介绍2021.7.30 Codeforces Round #723 (Div. 2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A
分析题意
给一个数组,求该数组的一个排列,满足每一项不等于相邻两项的平均值。
Example
inputCopy
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
outputCopy
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array [3,1,4,2,5,6] works, as it’s a permutation of [1,2,3,4,5,6], and 3+42≠1, 1+22≠4, 4+52≠2, 2+62≠5, 5+32≠6, 6+12≠3.
314256
142563
425631
256314
563142
631425

314256
由于是一个环形,所以怎么样都ok
如果再大一点就可以发现规律了
123456789
-》
1_2_3_4_5
_6_7_8_9
发现是不是这样就可以啦?
因为对任意一个数字来说,大于它的两个数字的算术平均数也一定大于ta
所以排序交替输出就好了

#include<bits/stdc++.h>
using namespace std;
int T;
int n;
int a[100010];
vector<int>ji;
vector<int>ou;
int main(){cin>>T;while(T--){cin>>n;for(int i=1;i<=2*n;i++)cin>>a[i];sort(a+1,a+1+2*n);int i=1;int j=n+1;for(int k=1;k<=2*n;k++){if(k&1){cout<<a[i]<<" ";i++;}else {cout<<a[j]<<" ";j++;}}cout<<endl;}
}

b题
以下都是错误的code

#include<bits/stdc++.h>
using namespace std;
int T;
int n;
int a[18];
int main(){cin>>T;while(T--){cin>>n;n%=111;n%=11;if(n==0)puts("YES");else puts("NO");}
}

#include<bits/stdc++.h>
using namespace std;
int T;
int n;
int a[18];
int main(){cin>>T;while(T--){cin>>n;int x=n%11;int b=n-11*x;int a=x-10*b;if(x!=11*a+111*b)puts("YES");else puts("NO");}
}

Accepted

#include<bits/stdc++.h>
using namespace std;
int T;
int n;
int a[18];string solve(int n){int b=n%11;n-=111*b;if(n<0)return "NO";n%=11;if(n==0)return "YES";else return "NO";
}int main(){cin>>T;while(T--){cin>>n;cout<<solve(n)<<endl;}
}

C1
This is the easy version of the problem. The only difference is that in this version n≤2000. You can make hacks only if both versions of the problem are solved.

There are n potions in a line, with potion 1 on the far left and potion n on the far right. Each potion will increase your health by ai when drunk. ai can be negative, meaning that potion will decrease will health.

You start with 0 health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.

What is the largest number of potions you can drink?

Input
The first line contains a single integer n (1≤n≤2000) — the number of potions.

The next line contains n integers a1, a2, … ,an (−109≤ai≤109) which represent the change in health after drinking that potion.

Output
Output a single integer, the maximum number of potions you can drink without your health becoming negative.

Example
inputCopy
6
4 -4 1 -3 1 -3
outputCopy
5
Note
For the sample, you can drink 5 potions by taking potions 1, 3, 4, 5 and 6. It is not possible to drink all 6 potions because your health will go negative at some point
看完题目,很明显是线性动态规划。
动态规划问题,首先要找到变化的状态有哪些。

这题的变化态有3个:
遇到的药水数,喝了的瓶数,生命值。

在整个过程中,只有上面三个量在变化。

然后我们注意到,题目要我们求在生命值不为负数的情况下最多能喝多少瓶。

设dp[i][j]表示前i瓶中喝j瓶能剩下的最大生命值,
那么状态转移方程:dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+a[i])

将初始值设为无穷小,dp[0]=0,再用滚动数组降维。

最后从后往前,dp[i]>=0的最大i值就是答案。
这个用滚动数组压缩到二维的没有过

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[20010];
int dp[2][20010];//表示前i瓶药水喝了j瓶药水时的最大健康值
signed main(){cin>>n;for(int i=1;i<=n;i++)cin>>a[i];memset(dp,-0x3f,sizeof dp);dp[0][0]=0;for(int i=1;i<=n;i++){for(int j=i;j>=1;j--){if(dp[(i-1)%2][j-1]+a[i]>=0)dp[i%2][j]=max(dp[(i-1)%2][j] , dp[(i-1)%2][j-1]+a[i]);else dp[i%2][j]=dp[(i-1)%2][j];}}for(int i=n;i>=1;i--){if(dp[n%2][i]>=0){cout<<i;return 0;}}}

这个直接压缩到一维的过掉了

#include<iostream>
#include<algorithm>
#define ll long long int
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
ll a[200005];
ll dp[200005];
int main()
{int n;cin>>n;for(int i=1;i<=n;i++) cin>>a[i];for(int i=1;i<=n;i++) dp[i]=-INF;dp[0]=0;for(int i=1;i<=n;i++){for(int j=i;j>0;j--){if(dp[j-1]+a[i]>=0){dp[j]=max(dp[j],dp[j-1]+a[i]);}}}for(int i=n;i>=0;i--){if(dp[i]>=0){cout<<i<<endl;return 0;}}
}

hard version把n扩大到20万了
不能用dp做
我们考虑贪心,使用优先队列大顶堆+反悔

#include<iostream>
#include<queue>
#define ll long long int
using namespace std;
priority_queue<ll> q;//大根堆 
int main()
{int n;cin>>n;ll s=0;ll ans=0;ll x;for(int i=0;i<n;i++){cin>>x;if(x>=0){s+=x;ans++;}else if(s+x>=0){s+=x;q.push(-x);ans++;}else if(!q.empty()){if(-x<q.top()){s+=x+q.top();q.pop();q.push(-x);}} }cout<<ans<<endl;return 0;
}

else if(!heap.empty()){int t=heap.top();if(-x<t)heap.pop(),heap.push(-x),xue+=t+x;}

这道题有点像勇者斗恶龙
好好吸收好好消化
这道题真的很好
谢谢这个博主,谢谢它的整理,真的学到很多

这篇关于2021.7.30 Codeforces Round #723 (Div. 2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/749674

相关文章

shell脚本自动删除30天以前的文件(最新推荐)

《shell脚本自动删除30天以前的文件(最新推荐)》该文章介绍了如何使用Shell脚本自动删除指定目录下30天以前的文件,并通过crontab设置定时任务,此外,还提供了如何使用Shell脚本删除E... 目录shell脚本自动删除30天以前的文件linux按照日期定时删除elasticsearch索引s

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

mysql-8.0.30压缩包版安装和配置MySQL环境过程

《mysql-8.0.30压缩包版安装和配置MySQL环境过程》该文章介绍了如何在Windows系统中下载、安装和配置MySQL数据库,包括下载地址、解压文件、创建和配置my.ini文件、设置环境变量... 目录压缩包安装配置下载配置环境变量下载和初始化总结压缩包安装配置下载下载地址:https://d

30常用 Maven 命令

Maven 是一个强大的项目管理和构建工具,它广泛用于 Java 项目的依赖管理、构建流程和插件集成。Maven 的命令行工具提供了大量的命令来帮助开发人员管理项目的生命周期、依赖和插件。以下是 常用 Maven 命令的使用场景及其详细解释。 1. mvn clean 使用场景:清理项目的生成目录,通常用于删除项目中自动生成的文件(如 target/ 目录)。共性规律:清理操作

2024网安周今日开幕,亚信安全亮相30城

2024年国家网络安全宣传周今天在广州拉开帷幕。今年网安周继续以“网络安全为人民,网络安全靠人民”为主题。2024年国家网络安全宣传周涵盖了1场开幕式、1场高峰论坛、5个重要活动、15场分论坛/座谈会/闭门会、6个主题日活动和网络安全“六进”活动。亚信安全出席2024年国家网络安全宣传周开幕式和主论坛,并将通过线下宣讲、创意科普、成果展示等多种形式,让广大民众看得懂、记得住安全知识,同时还

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja