Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解
A..B略
C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#define LL long long
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxx = 2e5+;
int a[maxx];
int vis[maxx];
LL k;
LL aa,bb;
int x,y,n;
bool check(int mid){
LL ans=;
int top=;
for (int i=;i<=mid;i++){
if (i%x== && i%y==){
vis[i]=a[++top];
ans=(LL)ans+vis[i]/*(aa+bb);
}else {
vis[i]=;
} }
for (int i=;i<=mid;i++){
if (i%y== && !vis[i]){
vis[i]=a[++top];
ans=(LL)ans+vis[i]/*bb;
} }
for (int i=;i<=mid;i++){
if (i%x== && !vis[i]){
vis[i]=a[++top];
ans=(LL)ans+vis[i]/*aa;
}
}
return ans>=k;
}
bool cmp(int x,int y){
return x>y;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for (int i=;i<=n;i++){
scanf("%d",&a[i]);
}
scanf("%d%d",&aa,&x);
scanf("%d%d",&bb,&y);
scanf("%lld",&k);
if (aa>bb){
swap(x,y);
swap(aa,bb);
}
sort(a+,a++n,cmp);
int l=;
int r=n;
int ans=-;
while(l<=r){
int mid=(l+r)>>;
if (check(mid)){
ans=mid;
r=mid-;
}else {
l=mid+;
}
}
if (ans==-){
printf("-1\n");
}else {
printf("%d\n",ans);
}
}
return ;
}
D 记录一次出现和最后一次出现的,考虑移动的比较麻烦,我们可以考虑对于两个相邻的大小的值,前面那个值最后次出现的位置是大于这个值第一次出现的,那么他们的相对位置关系是不用移动的,我们求出这种不用移动的相对位置连续个数是最多的,那么就是最长不用移动的,其他的肯定要移动,那么直接用所有值的个数去减这个值就行了。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#define LL long long
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxx = 2e5+;
int a[maxx];
int vis[maxx];
LL k;
LL aa,bb;
int x,y,n;
bool check(int mid){
LL ans=;
int top=;
for (int i=;i<=mid;i++){
if (i%x== && i%y==){
vis[i]=a[++top];
ans=(LL)ans+vis[i]/*(aa+bb);
}else {
vis[i]=;
} }
for (int i=;i<=mid;i++){
if (i%y== && !vis[i]){
vis[i]=a[++top];
ans=(LL)ans+vis[i]/*bb;
} }
for (int i=;i<=mid;i++){
if (i%x== && !vis[i]){
vis[i]=a[++top];
ans=(LL)ans+vis[i]/*aa;
}
}
return ans>=k;
}
bool cmp(int x,int y){
return x>y;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for (int i=;i<=n;i++){
scanf("%d",&a[i]);
}
scanf("%d%d",&aa,&x);
scanf("%d%d",&bb,&y);
scanf("%lld",&k);
if (aa>bb){
swap(x,y);
swap(aa,bb);
}
sort(a+,a++n,cmp);
int l=;
int r=n;
int ans=-;
while(l<=r){
int mid=(l+r)>>;
if (check(mid)){
ans=mid;
r=mid-;
}else {
l=mid+;
}
}
if (ans==-){
printf("-1\n");
}else {
printf("%d\n",ans);
}
}
return ;
}
E. Paint the Tree
我们考虑dp[i][0]代表这个节点的k重颜色已经全部匹配,dp[i][0代表当前节点还有颜色没有匹配。那么我们其实可以很容易得到dp的转移
首先dp[i][1]+=sigma[son[i]][0] dp[i][0]+=sigma[son[i]][0] 也就是说我们每个节点首先看出全部选了子树颜色已经全部匹配的结果
我们再把所有节点选择子树没有匹配完全的加上这条路径的长度去减去子树已经匹配的完全的差值,然后再差值中选中前k大的正数和加到dp[i][0]表示选择后悔了
把前i-1的正数加到dp[i][1]中即可。最后取根节点两者的最大值即可。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define LL long long
using namespace std;
const int maxx = 5e5+;
int ver[maxx*],edge[*maxx],head[maxx],Next[*maxx];
int n,k,tot;
LL dp[maxx][];
void add(int x,int y,int w){
ver[++tot]=y;edge[tot]=w;Next[tot]=head[x];head[x]=tot;
ver[++tot]=x;edge[tot]=w;Next[tot]=head[y];head[y]=tot;
}
bool cmp(int a,int b){
return a>b;
}
void dfs(int u,int fa){
dp[u][]=dp[u][]=;
vector<int>vv;
vv.clear();
for (int i=head[u];i;i=Next[i]){
int v=ver[i];
if (v==fa)continue;
dfs(v,u);
dp[u][]+=dp[v][];
dp[u][]+=dp[v][];
}
for (int i=head[u];i;i=Next[i]){
int v=ver[i];
if (v==fa)continue;
vv.push_back(dp[v][]+edge[i]-dp[v][]);
}
sort(vv.begin(),vv.end(),cmp);
int sz=min((int)vv.size(),k);
for (int i=;i<sz;i++){
if(vv[i]<)break;
if(i==k-){
dp[u][]=(LL)vv[i]+dp[u][];
}else {
dp[u][]=(LL)vv[i]+dp[u][];
dp[u][]=(LL)vv[i]+dp[u][];
}
}
}
int main(){
int t;
scanf("%d",&t);
int uu,vv,ww;
while(t--){
scanf("%d%d",&n,&k);
for (int i=;i<=n;i++){
head[i]=;
}
for (int i=;i<=n-;i++){
scanf("%d%d%d",&uu,&vv,&ww);
add(uu,vv,ww);
}
dfs(,-);
printf("%lld\n",max(dp[][],dp[][]));
}
return ;
}
Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解的更多相关文章
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) C. Save the Nature【枚举二分答案】
https://codeforces.com/contest/1241/problem/C You are an environmental activist at heart but the rea ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) D. Sequence Sorting
链接: https://codeforces.com/contest/1241/problem/D 题意: You are given a sequence a1,a2,-,an, consistin ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) B. Strings Equalization
链接: https://codeforces.com/contest/1241/problem/B 题意: You are given two strings of equal length s an ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) C. Save the Nature
链接: https://codeforces.com/contest/1241/problem/C 题意: You are an environmental activist at heart but ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) A. CME
链接: https://codeforces.com/contest/1241/problem/A 题意: Let's denote correct match equation (we will d ...
- Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1)
Virtual participate 的,D题不会做,打了1:30就打不动了,过了ABCE. A - CME 题意:? 题解:? void test_case() { int n; scanf(&q ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2)
A - Forgetting Things 题意:给 \(a,b\) 两个数字的开头数字(1~9),求使得等式 \(a=b-1\) 成立的一组 \(a,b\) ,无解输出-1. 题解:很显然只有 \( ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3
A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...
- 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...
随机推荐
- Kubernetes 基于 ubuntu18.04 手工部署 (k8s)
由于工作的需要, 手工部署一个 Kubernetes 环境(k8s).(以前都是云上搞定,拿来用) 习惯把这种工作记录下来,自己备查也和别人分享 网上相关文章很多, 我也参考了很多,这里推荐一个 链接 ...
- 实用Jupyter Notebook扩展工具——提升你的工作效率
Jupyter Notebook 现已成为数据分析,机器学习的必备工具.因为它可以让数据分析师集中精力向用户解释整个分析过程.通过安装一些扩展工具,可以让你在Jupyter Notebook上的工作效 ...
- python学生管理系统
import osimport re #获取本机用户名,构建student.txt文件名创建在左面import getpassusername=getpass.getuser()print(" ...
- BZOJ1452 [JSOI2009]Count [2017年4月计划 树状数组02]
1452: [JSOI2009]Count Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2419 Solved: 1403[Submit][Stat ...
- 【产品经理】产品经理不懂API接口是什么,怎么和程序员做朋友?
接口不是技术经理来写吗?没接过它,一脸不清楚地节奏 开放即共享,是互联网的一个重要属性和精神.它是一种服务模式,一个特殊的产品,目前较大规模的互联网企业都有自己的开放平台. 如果把自己局限为一个功能产 ...
- js 正则去除html代码
function delHtmlTag(str){ return str.replace(/<[^>]+>/g,"");//去掉所有的html标记 }
- cf round 482D Kuro and GCD and XOR and SUM
题意: 开始有个空集合,现在有两种操作: $(1,x)$:给集合加一个数$x$,$x \leq 10^5$; $(2,x,k,s)$:在集合中找一个$a$,满足$a \leq s-x$,而且$k|gc ...
- PyCharm如何删除工程项目
1.在菜单中选择:file——>close project 2.选择需要删除的项目右上角的“×”号进行删除工程项目 3.找到工程项目的存放路径,删除对应的工程项目文件 通过上诉操作即可在pych ...
- Linux iptables开放特定端口
如果系统已安装则调过安装步骤 查找安装包 yum list | grep iptables 安装iptables yum install iptables-services 重启防火墙使配置文件生效 ...
- Linux下安装zookeeper-3.4.13
转载至:https://yq.aliyun.com/articles/662422 1.zookeeper官网下载安装包http://mirrors.hust.edu.cn/apache/zookee ...