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 ...
随机推荐
- 【笔记】LR11中关联设置
LR中关联建议都手动进行,自动不好用,也容易出错. 在LR中我们什么要做关联:1.关联解决的是动态数据的参数化.2.关联的数据一定是服务器响应的数据.3.服务器响应过来的数据在后面的服务还要使用. 手 ...
- java并发系列(六)-----Java并发:volatile关键字解析
在 Java 并发编程中,要想使并发程序能够正确地执行,必须要保证三条原则,即:原子性.可见性和有序性.只要有一条原则没有被保证,就有可能会导致程序运行不正确.volatile关键字 被用来保证可见性 ...
- 关于springmvc 只能在index.jsp页面显示图片的处理办法jsp页面无法显示图片
首先,已经配置好了mvc对静态资源的处理 只有index,jsp可以显示图片 其他页面同样的代码则不显示 后来折腾了半天,发现 index是static的父目录的级别文件 可以向下访问 但是其他的js ...
- MacBook下为要运行的.net core 项目指定sdk版本
安装完.net core 3.0,运行早期版本构建的项目遇到运行错误,查阅官方文档解决问题,特此记录!官方原文如下: SDK 使用最新安装的版本 SDK 命令包括 dotnet new 和 dotne ...
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- html 遮罩层以及弹出框的制作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- line-height的用法(一)
行高”顾名思意指一行文字的高度.具体来说是指两行文字间基线之间的距离. 从上到下四条线分别是顶线.中线.基线.底线,很像才学英语字母时的四线三格,我们知道vertical-align属性中有top.m ...
- 学习es6
#第一节 初始化项目 npm init -y 安装babel-cli npm install -g babel-cli npm install --save-dev babel-preset-es20 ...
- delete records in table A not in table B
转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低 ...
- JSP Web第六章整理复习 JavaBean技术
P183 什么是JavaBean,JavaBean有哪些特点? javabean是一种特殊的java类 特点:属性private,方法public P184 JavaBean封装数据,例6-1,6-2 ...