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. [转]web计时机制——performance对象

    页面性能一直都是Web开发人员比较关注的领域.但在实际应用中,度量页面性能的指标,是javascript的Date对象.Web Timing API改变了这个局面,让开发人员通过javascript就 ...

  2. 20190725-Silly

    $ \mathsf{You\ think\ about\ what\ you\ want\ because\ you're\ just\ alive}$ ——C418-Alive 我不能yuanlia ...

  3. Ubuntu 16.04 配置 L2tp 客户端

    #install lib -dev libsecret--dev libgtk--dev libglib2.-dev xl2tpd strongswan #install network-manage ...

  4. OpenLayers测量距离和面积

    <!DOCTYPE html> <html> <head> <title>测量距离和面积</title> <meta http-equ ...

  5. python 读取xml文件

    首先,获得标签信息abc.xml <?xml version="1.0" encoding="utf-8"?> <catalog> &l ...

  6. P5562 [Celeste-B]Center of the Earth 题解

    构造 因为题目只要求两位相同,所以可以暴力枚举这两位所有的可能性,方案数为\(O(n^2)\). 但是,这么做是显然不优的,因为完全没有用到第三位. 观察题目条件:n为偶数. 就想一想能不能奇数偶数分 ...

  7. js面向对象开发基础

    js的面向对象开发能力较弱,基本是以prototype为核心的面向对象,虽然现在出了个class这玩意,但本文还是先不做探讨. 面向对象基础——构造函数方法 var Fly = function (s ...

  8. LintCode_469 等价二叉树

    题目 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等. 样例 1 1 / \ / \ 2 2 and 2 2 / / 4 4 就是两棵等 ...

  9. objectarx之判断给定的三点是否共线

    bool ThreePointIsCollinear(const AcGePoint2d &pt1, const AcGePoint2d &pt2, const AcGePoint2d ...

  10. settings.gradle与build.gradle有什么区别

    参考回答 settings.gradle文件是gradle项目的总体配置文件,一般会把子项目中通用的一些配置放在这个文件中,有点雷士与maven的parent pom 文件. build.gradle ...