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 ...
随机推荐
- 【linux配置】Redhat6.5基础配置指南
Redhat6.5基础配置指南 本文针对鄙人在工作中常用系统配置加以说明,通常公司用于生产和测试的服务器基本上都不能连接外网,需要针对刚刚安装好的系统做一系列的常用配置. 一.常用基础系统配置 1.关 ...
- CesiumLab V1.3 新功能 MAX场景处理(免费Cesium处理工具集)
每次到写文章的时候就很高兴,意味着又有重大功能更新了,也意味着10多天昏天黑地的闭关日子暂时结束了. 依照惯例,先放图 小范围精模型cesium加载效果 大范围白模cesium加载效果 ...
- VUE打包好的文件部署让beego实现静态文件访问,如何用根目录来访问静态文件?
最近的一个全栈项目,光伏云监控系统,后端使用beego框架,纯api,前端使用VUE2.0.项目地址:http://scada.ssechina.com:88/static 我把打包好的前端文件放到g ...
- yield和return
yield 是用于生成器.什么是生成器,你可以通俗的认为,在一个函数中,使用了yield来代替return的位置的函数,就是生成器.它不同于函数的使用方法是:函数使用return来进行返回值,每调用一 ...
- Spring 集成 Redis
pom.xml <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...
- day36 10-Hibernate中的事务:解决丢失更新
演示hibernate如何产生丢失更新的 丢失更新是怎么玩的?首先两个事务先都把它查出来. A事务里面去修改的数据没了,被B事务覆盖掉了.这是被B事务提交覆盖,B事务回滚也能覆盖.这就是丢失更新的效果 ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- golang之switch
- webpack 4.X 创建 react项目
1. 新建一个文件夹2. 运行 npm init -y 快速初始化项目3. 在跟目录创建src文件夹 dist文件夹4. 在src下创建 index.html main.js // index.htm ...
- HR招聘_(二)_招聘方法论(招聘原因及原则)
1 招聘原因 离职 转岗 新增 工作量加大而无法负荷(若为短期工作量的加大可考虑外包或临时雇员) 业务发展需求(新产品线拓展,新事业部组建或组织架构变化等) 2 招聘原则 平等 面试官和候选人双方地位 ...