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) 题解的更多相关文章

  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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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. 题解:很显然只有 \( ...

  8. 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) 思路:我们想一下如果题目说的是最 ...

  9. 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...

随机推荐

  1. redis异常-MISCONF Redis is configured to save RDB snapshots

     在eclipse中用java代码通过jedis操作redis的时候,报这个错:   redis.clients.jedis.exceptions.JedisDataException: MISCON ...

  2. angular和vue的对比学习之路

    vue-ng 打开vue的中文官网一段关于vue的描述 HTML 模板 + JSON 数据,再创建一个 Vue 实例,就这么简单. 那我么再看下angular中文网 AngularJS是为了克服HTM ...

  3. Python各种转义符

    文章来源:https://www.cnblogs.com/luckyplj/p/9792658.html 谢谢作者:雨后观山色

  4. python自动化---各类发送邮件方法及其可能的错误

    一.发送文本邮件 可能的问题1.:需要注意,目前QQ邮箱来讲,不能收到完整的邮件,即有些内容不能显示,最好全部使用网易邮箱: 可能的问题2.:在以往的文本邮件发送中,只写了 msg = MIMETex ...

  5. react仿豆瓣

    最近公司在做一个自己内部的图片上传系统,目的是帮助设计人员方便上传图片,用的是vue技术,但是说到vue,可能要提到更早出来的react,react是facebook搞的一套语法糖,也是革命性的用组件 ...

  6. 【洛谷P2907】 【USACO08OPEN】农场周围的道路 水模拟分治

    P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm 题目描述 Farmer John's cows have taken an interest in ex ...

  7. Leetcode59. Spiral Matrix II螺旋矩阵2

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

  8. 怎么让一个不定宽高的div垂直水平居中?

    方法一:使用CSS3 transform 父盒子设置:position:relative; div设置:position:absolute;transform:translate(-50%,-50%) ...

  9. 【等价的穿越】Burnside引理&Pólya计数法

    Problem 起源: SGU 294 He's Circle 遗憾的是,被吃了. Poj有道类似的: Mission 一个长度为n(1≤n≤24)的环由0,1,2组成,求有多少本质不同的环. 实际上 ...

  10. vue-cli3 搭建的前端项目基础模板

    基于 vue-cli3 搭建的前端模板,fork 或 clone 本仓库,即可搭建完成一个新项目的基础模板,源码地址,欢迎 star 或 fork 特性 CSS 预编译语言:less Ajax: ax ...