Codeforces Round #527 (Div. 3)F(DFS,DP)】的更多相关文章

#include<bits/stdc++.h>using namespace std;const int N=200005;int n,A[N];long long Mx,tot,S[N];vector<int>Adj[N];void DFS(int v,int p){    S[v]=A[v];    for(int &u:Adj[v])        if(u!=p)            DFS(u,v),S[v]+=S[u];    if(p)//0号结点是不存在的…
#define HAVE_STRUCT_TIMESPEC#include <bits/stdc++.h>using namespace std;int val[300007],num[300007],ans=0;vector<int>v[300007];void dfs(int u){ if(v[u].size()==0){ num[u]=1; ans++; return; } int tmp=0; for(int i=0;i<v[u].size();i++){ int vv…
D. Dreamoon and Binary time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Dreamoon saw a large integer x written on the ground and wants to print its binary form out. Dreamoon has accomplishe…
C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining string s' as a…
Tetrahedron time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tetrahedron. Let's mark its vertices with letters A, B, C and D correspondingly. An ant is standing in the verte…
#include<bits/stdc++.h>using namespace std;const long long mod=998244353;int n;int a[100007];long long dp[100007][207][3];//第i位值为j时k是否成立,k=0,i<i-1,k=1,i==i-1,k=2,i>i-1 int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++)   …
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree consisting exactly of nn vertices. Tree is a…
B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it…
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree…
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an array aa consisting of nn integers. Beauty of array i…
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying completely. Today, the E…
#include<bits/stdc++.h>using namespace std;const int maxn=1e6+7;pair<string,int>p[maxn];int nn,n;int cmp(pair<string,int>a,pair<string,int>b){    return a.first.length()<b.first.length();}char ans[maxn];multiset<string>sst…
题目链接 题意:给你一棵树,让你找一个顶点iii,使得这个点的∑dis(i,j)∗a[j]\sum dis(i,j)*a[j]∑dis(i,j)∗a[j]最大.dis(i,j)dis(i,j)dis(i,j)为iii到jjj的距离. 思路:题解还是好看啊 先从1开始搜,预处理出当1为根的时候的答案记为resresres,递归的过程中假设当前节点为iii,那么sum[i]sum[i]sum[i]数组的意义就是:从以当前顶点为根的所有子树的所有节点的权值和记为sum[i]sum[i]sum[i].…
#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){    int n;    int mn=0;    scanf("%d",&n);    for(int i=1;i<=n;i++){        scanf("%d",&a[i]);        if(a[i]>mn)            mn=a…
F. Economic Difficulties An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea! Each grid (main and reserve) has a head node (its num…
链接:http://codeforces.com/contest/437/problem/A A. The Child and Homework time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Once upon a time a child got a test consisting of multiple-choice qu…
http://codeforces.com/contest/426/problem/B B. Sereja and Mirroring time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's assume that we are given a matrix b of size x × y, let's determine…
题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k.(k>=0) 思路:昨晚打cf因为某些原因,沉不下心来看题,本来是个上分的好机会QAQ...所以吸取教训,下次状态好的时候再打比赛.    回到题目,首先给出gcd(a,b)=gcd(a,a-b),这个很显然,所以有gcd(a+k,b+k)=gcd(a+k,a-b).而lcm(a+k,b+k)=(a+…
NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: It turned out that the circles are equal. NN was very…
实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using namespace std; ]; ][]; ]; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; ; ; ;i<=n;++i){ cin>>a[i]; in…
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. The…
很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找出a和b中最小的那个,然后开始枚举,一直枚举到sqrt(6*n)的向上取整.这样所有可能是答案的情况都有啦.再干别的都是重复的或者肯定不是最小面积的. #include<iostream> #include<cstdio> #include<cstdlib> #includ…
#include<bits/stdc++.h>using namespace std;int a[5007];int dp[5007][5007];int main(){    int n; scanf("%d",&n);    for(int i=1;i<=n;i++)        scanf("%d",&a[i]);    n=unique(a+1,a+1+n)-a-1;    for(int i=n;i>=1;i--)…
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; vector<char>ans; ][]; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ';++i) ans.emplace_back(i); for(int i='A';i<='Z';++i) ans.emplace_back(i…
题意:给你一个长度为\(n\)的序列,求一个最长的\({x,x+1,x+2,.....,x+k-1}\)的序列,输出它的长度以及每个数在原序列的位置. 题解:因为这题有个限定条件,最长序列是公差为\(1\)的单增序列,所以其实非常简单. ​ 我们用\(map\)来记录每个元素最后出现的位置,\(dp\)表示当前位置的最长序列,\(last\)表示\(x-1\)的位置. ​ \(dp\)状态有两种更新方式: 1.如果上个元素\((x-1)\)存在,则\(dp[i]=dp[x-1]+1\). ​ 2…
首先膜一发网上的题解.大佬们tql. 给你n个单调递增的数字,问是否能够把这些数字重新构成一棵二叉搜索树(BST),且所有的父亲结点和叶子结点之间的gcd > 1? 这个题场上是想暴力试试的.结果不行.发现符合最优子结构,可以DP.但是想不出来怎么转移. 看了大佬的题解. 若以第 k 个结点为根节点,用 L[i][k] 表示是否能够向左延伸到 i 点,R[k][j] 表示是否能够向右延伸到 j 点. 那么区间 [l, r] 合法,仅当 L[l][k] && R[k][r] == 1.…
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入n,k,n表示字符串的长度,k表示从1-k的小写字符(1即是a),现在要求最大化最少字符的数量. 题解: 贪心搞一搞就行了. 代码如下: #include <bits/stdc++.h> using namespace std; int T; int n,k; int main(){ cin>…
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Description You are given a set of size $m$ with integer elements between $0$ and $2^{n}-1$ inclusive. Let's build an undirected graph on these integers in…
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can…
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/problem/F Solution 设\(v_i\)表示第\(i\)个点的果子数,设\(b_i=v_i-\sum_{x\in son}v_x\),显然依题意要满足\(b_i\geqslant 0\). 根据差分的性质我们可以得到\(\sum b_i=x\). 假设我们硬点树上剩下了\(m\)个点,则…