Codeforces Round #530 (Div. 1)
A - Sum in the tree
就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值。
#include<bits/stdc++.h>
using namespace std;
char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin))?EOF:*p1++;
return getchar();
}
template<class T>
int read(T &ans) {
T f=1;ans=0;
char ch=gc();
while(!isdigit(ch)) {
if(ch==EOF) return EOF;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
}
template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)==EOF?EOF:read(b);
}
template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)==EOF?EOF:read(c);
}
typedef long long ll;
const int Maxn=1100000;
const int inf=0x3f3f3f3f;
const ll mod=1000000007;
int s[Maxn],to[Maxn],nxt[Maxn],first[Maxn],tot=1;
int n,x,flag;
ll ans;
inline void add(int u,int v) {
to[tot]=v;
nxt[tot]=first[u];
first[u]=tot++;
}
void dfs(int root) {
if(s[root]==-1) {
int temp=inf;
for(int i=first[root];i;i=nxt[i]) {
dfs(to[i]);
temp=min(temp,s[to[i]]);
}
s[root]=temp;
for(int i=first[root];i;i=nxt[i]) {
ans+=s[to[i]]-s[root];
}
}
else {
for(int i=first[root];i;i=nxt[i]) {
dfs(to[i]);
if(s[to[i]]==inf) s[to[i]]=s[root];
if(s[to[i]]<s[root]) flag=1;
ans+=s[to[i]]-s[root];
}
}
}
int main() {
read(n);
for(int i=2;i<=n;i++) {
read(x);
add(x,i);
}
for(int i=1;i<=n;i++) read(s[i]);
dfs(1);ans+=s[1];
if(flag) return 0*puts("-1");
printf("%I64d",ans);
return 0;
}
B - Nice table
这个结论就是要么是对于每一行都只有两种字符或每一列都只有两种字符。至于为什么。。自己感性理解一下就好了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#include<string>
#include<iostream>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
using namespace std;
inline char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
}
template<class T>
int read(T &ans) {
ans=0;char ch=gc();T f=1;
while(!isdigit(ch)) {
if(ch==EOF) return -1;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
}
template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}
template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}
typedef long long ll;
const int Maxn=310000;
const int inf=0x3f3f3f3f;
char t[4]={'A','T','C','G'};
string a[Maxn],b[Maxn];
int w[Maxn][4][4],w1[Maxn][4][4];
int p[4],p1[4],p2[4],n,m;
inline bool check() {
return p[1]!=1||p[2]!=2||p[3]!=3||p[0]!=0;
}
int calc() {
int ans=0;
for(int i=0,t=2;i<m;i++,t^=2)
ans+=min(w[i][p[0+t]][p[1+t]],w[i][p[1+t]][p[0+t]]);
return ans;
}
signed main() {
// freopen("test.in","r",stdin);
read(n,m);
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<m;i++)
for(int x=0;x<4;x++)
for(int y=0;y<4;y++)
for(int j=0;j<n;j++)
if(j&1) w[i][x][y]+=(t[y]!=a[j][i]);
else w[i][x][y]+=(t[x]!=a[j][i]);
int ans1=inf,ans2=inf;
for(int i=0;i<4;i++) p[i]=i;
do {
int temp=calc();
if(temp<ans1) {
ans1=temp;
memcpy(p1,p,sizeof(p));
}
}
while(next_permutation(p,p+4),check());
for(int i=0;i<m;i++) b[i].resize(n);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) b[j][i]=a[i][j];
memcpy(w1,w,sizeof(w1));
memset(w,0,sizeof(w));
swap(n,m);
for(int i=0;i<m;i++)
for(int x=0;x<4;x++)
for(int y=0;y<4;y++)
for(int j=0;j<n;j++)
if(j&1) w[i][x][y]+=(t[y]!=b[j][i]);
else w[i][x][y]+=(t[x]!=b[j][i]);
do {
int temp=calc();
if(temp<ans2) {
ans2=temp;
memcpy(p2,p,sizeof(p));
}
}
while(next_permutation(p,p+4),check());
swap(n,m);
if(ans1<ans2)
for(int i=0;i<n;i++,puts(""))
for(int j=0;j<m;j++)
if(j&1) putchar(t[p1[(w1[j][p1[0]][p1[1]]>w1[j][p1[1]][p1[0]])^(i&1)]]);
else putchar(t[p1[(w1[j][p1[2]][p1[3]]>w1[j][p1[3]][p1[2]])^(i&1)+2]]);
else
for(int i=0;i<n;i++,puts(""))
for(int j=0;j<m;j++)
if(i&1) putchar(t[p2[(w[i][p2[0]][p2[1]]>w[i][p2[1]][p2[0]])^(j&1)]]);
else putchar(t[p2[(w[i][p2[2]][p2[3]]>w[i][p2[3]][p2[2]])^(j&1)+2]]);
return 0;
}
C - Construct a tree
首先就是分叉数越大,其对应的所有子树的大小和越小。那么依次枚举判断,如果合法构造即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
using namespace std;
inline char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
}
template<class T>
int read(T &ans) {
ans=0;char ch=gc();T f=1;
while(!isdigit(ch)) {
if(ch==EOF) return -1;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
}
template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}
template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}
typedef long long ll;
const int Maxn=110000;
const int inf=0x3f3f3f3f;
int n,a[Maxn],p[Maxn];
ll s;
ll check(int x) {
ll res=n,temp=1,ans=0;
for(int i=1;res;i++) {
qmin(temp,res);
ans+=temp*i;
res-=temp;
temp*=x;
}
return ans;
}
void work(int x) {
a[1]=1;s--;
ll res=n-1,temp=x,t=2;
for(;res;t++) {
qmin(temp,res);
s-=temp*t;
a[t]=temp;
res-=temp;
temp*=x;
}
for(int i=t-1;s;i--) {
while(a[i]>1) {
a[i]--;
s+=i;
if(s>t) {
s-=t;
a[t++]++;
}
else {
a[s]++;
s=0;
break;
}
}
}
int sum=0,tempp=1;
for(int i=1;i<t;i++) {
int temp=sum+1;
sum=tempp;
for(int j=temp;j<=sum;j++)
for(int k=min(a[i+1],x);k;k--) {
p[++tempp]=j;
a[i+1]--;
}
}
for(int i=2;i<=n;i++) printf("%d ",p[i]);
}
signed main() {
// freopen("test.in","r",stdin);
read(n,s);
if(1ll*n*(n+1)/2==s) {
puts("Yes");
for(int i=2;i<=n;i++) printf("%d ",i-1);
return 0;
}
if(1ll*n*(n+1)/2<s||2*n-1>s) {
puts("No");
return 0;
}
for(int i=2;i<=n;i++) if(check(i)<=s) {
puts("Yes");
work(i);
return 0;
}
return 0;
}
Codeforces Round #530 (Div. 1)的更多相关文章
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #530 (Div. 2) A,B,C,D
A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...
- Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)
D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #530 (Div. 2) C D
C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...
- Codeforces Round #530 (Div. 2)
RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...
- Codeforces Round #530 Div. 1 自闭记
A:显然应该让未确定的大小尽量大.不知道写了啥就wa了一发. #include<iostream> #include<cstdio> #include<cmath> ...
- Codeforces Round #530 (Div. 2) F - Cookies
F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...
随机推荐
- php 中date显示时间不对与Linux文件乱码问题
php 中date显示时间不对解决办法如下1.修改/etc/php.ini文件 在里头中找到data.timezone =去掉它前面的分号';' 然后设置data.timezone = “Asia/S ...
- 【Python】小练习
1.python爬虫 (1)抓取一个新闻网上含有某一关键字的新闻,http://internasional.kompas.com/就是这个网站上面所有内容含有THAAD这个关键词的新闻 (2)爬取大众 ...
- 非极大值抑制(NMS)
非极大值抑制顾名思义就是抑制不是极大值的元素,搜索局部的极大值.这个局部代表的是一个邻域,邻域有两个参数可变,一个是邻域的维数,二是邻域的大小.这里不讨论通用的NMS算法,而是用于在目标检测中提取分数 ...
- quic协议实时视频直播
扫盲 https://www.jianshu.com/p/b7546ff9b683 demo https://github.com/felix-001/QuicRtmp https://github. ...
- EM算法小结
一.什么是EM算法? EM算法是机器学习中一个很重要的算法,即期望最大化算法,主要包括以下两个步骤: E步骤:estimate the expected values M步骤:re-estimate ...
- 你应该知道的最好Webmail邮件客户端,
1 . Kite Kite is an opensource replacement to Gmail. Kite is a webmail designed to look a lot like g ...
- Oracle的FIXED_DATE参数
今天发现一个有意思的问题, 我们知道,在Oracle数据库中正常执行 select sysdate from dual 都可以返回当前主机的系统时间. 正常修改系统时间,对应的查询结果也会变成修改后的 ...
- VMware Coding Challenge: The Heist
类似BackpackII问题 static int maximize_loot(int[] gold, int[] silver) { int[][] res = new int[gold.lengt ...
- #C语言初学记录(位运算)
位运算 Problem Description7-1 数组元素循环右移问题 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由 ...
- mysql回滚日志
一.回滚日志(undo log) 1.作用 保存了事务发生之前的数据的一个版本,可以用于回滚,同时可以提供多版本并发控制下的读(MVCC),也即非锁定读 2.内容 逻辑格式的日志,在执行undo的时候 ...