rated

Educational Codeforces Round 78 (Rated for Div. 2)E(构造,DFS)

↘锁芯ラ 提交于 2020-01-14 19:07:11
DFS,把和当前结点相连的点全都括在当前结点左右区间里,它们的左端点依次++,然后对这些结点进行DFS,优先对左端点更大的进行DFS,这样它右端点会先括起来,和它同层的结点(后DFS的那些)的区间会把它括起来,这样它们就不会相交了。 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int cnt=1; 5 vector<int>v[500007]; 6 int l[500007],r[500007]; 7 void dfs(int x,int fa){ 8 for(int i=0;i<v[x].size();++i) 9 if(v[x][i]!=fa) 10 l[v[x][i]]=++cnt; 11 r[x]=++cnt; 12 for(int i=v[x].size()-1;i>=0;--i) 13 if(v[x][i]!=fa) 14 dfs(v[x][i],x); 15 } 16 int main(){ 17 ios::sync_with_stdio(false); 18 cin.tie(NULL); 19 cout.tie(NULL); 20 int n; 21 cin>>n; 22 for(int i=1;i<n;++i){ 23 int x,y; 24

Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)

拈花ヽ惹草 提交于 2020-01-02 04:00:32
题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序。    输出最小操作次数 dp: #include<bits/stdc++.h> using namespace std; const int M=2e5+5; int dp[M][3]; int a[M]; ///dp[i][0]表示前i个数全属于第一个数组所要花费的最小代价 ///dp[i][1]表示前i个数全属于第一、二个数组所要花费的最小代价 ///!!dp[i][1]必须要保证前i个数是要连续的属于第一个数组,和连续的属于第一个数组前后分布 int main(){ int k1,k2,k3; scanf("%d%d%d",&k1,&k2,&k3); int n=k1+k2+k3; for(int i=1,x;i<=k1;i++){ cin>>x; a[x]=0; } for(int i=1,x;i<=k2;i++){ cin>>x; a[x]=1; } for(int i=1,x;i<=k3;i++){ cin>>x; a[x]=2; } for(int i=1;i<=n;i++){ dp[i][0]=dp[i-1][0]+(a[i]==0?0:1); dp[i][1]

Educational Codeforces Round 55 (Rated for Div. 2)

你说的曾经没有我的故事 提交于 2019-12-31 21:44:50
A. Vasya and Book 题意:翻书,从x到y,每次翻d页,边缘不会翻过,求最小翻书次数 题解:只有三种方式,暴力就好,注意abs(x-y) #include<bits/stdc++.h> using namespace std; const int INF=1e9+10; int main(){ int t,n,x,y,d;scanf("%d",&t); while(t--){ scanf("%d%d%d%d",&n,&x,&y,&d); int ans=INF; if((y-x)%d==0) ans=min(ans,abs(y-x)/d); int tmp=(x-1)/d+((x-1)%d!=0); if((y-1)%d==0) ans=min(ans,tmp+(y-1)/d); tmp=(n-x)/d+((n-x)%d!=0); if((n-y)%d==0) ans=min(ans,tmp+(n-y)/d); if(ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0; } View Code B. Vova and Trophies 题意:01串,考虑交换一次两个1的位置,求最长的连续1的个数 题解:因为只能交换一次,所以记录每一个S的位置,然后讨论间隔就好,注意内部交换 #include

Educational Codeforces Round 79 (Rated for Div. 2)(Aabc字符串排列,B枚举+二分,C线段树+栈,D概率dp)

拟墨画扇 提交于 2019-12-28 17:18:48
题目链接 A. New Year Garland 给你a,b,c,值代表a个A字符,b个B字符,c个C字符。。 问能否构造一个相邻两个字符都不相同的字符串,所有字符都必须用完。 做法:三个值从小到大排序,最大的值应该是这样分布的:A A A B B B B 那么c的最大值就是a+b+1 c取值最小值是这样分布的:B A B A B A B 那么C必须是大于等于b-a-1 #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=(b);++i) #define mem(a,x) memset(a,x,sizeof(a)) #define pb push_back #define pi pair<int, int> #define mk make_pair using namespace std; typedef long long ll; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} int a[10]; int main() { int n=3; int _;cin>>_;while(_--) { for(int i=1;i<=n;++i){ scanf("%d",&a[i]); } sort(a+1,a+1+n); if(a[2]-a[1]-1<=a[3]&&a[3]<=a[1]

Educational Codeforces Round 64 (Rated for Div. 2)-C. Match Points

杀马特。学长 韩版系。学妹 提交于 2019-12-27 08:17:21
C. Match Points time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given a set of points x1, x2, ..., xn on the number line. Two points i and j can be matched with each other if the following conditions hold: neither i nor j is matched with any other point; |xi−xj|≥z. What is the maximum number of pairs of points you can match with each other? Input The first line contains two integers n and z (2≤n≤2⋅105, 1≤z≤109) — the number of points and the constraint on the distance between matched points, respectively. The second line

Educational Codeforces Round 69 (Rated for Div. 2)D(DP,思维)

二次信任 提交于 2019-12-27 07:18:39
#include<bits/stdc++.h> using namespace std; int a[300007]; long long sum[300007],tmp[300007],mx[300007]; int main(){ int n,m,k; cin>>n>>m>>k; for(int i=1;i<=n;++i){ cin>>a[i]; sum[i]=sum[i-1]+a[i];//前缀和 } long long ans=0; for(int i=1;i<=m;++i){//枚举起点,每次可以向右移动m位所以复杂度为O(m*) for(int j=i;j<=n;++j) tmp[j]=sum[j]-1ll*((j-i)/m+1)*k;//以i为起点,j为终点,题意中需要求得的最大的数 mx[n]=tmp[n]; for(int j=n-1;j>=i;--j) mx[j]=max(tmp[j],mx[j+1]);//保留j以前包括j位置最大的题意中需要求得的最大的数 for(int j=i;j<=n;j+=m) ans=max(ans,mx[j]-(sum[j-1]-1ll*(j-i)/m*k));//将i每次向右移动m位,更新答案 } cout<<ans; return 0; } 来源: https://www.cnblogs.com/ldudxy/p

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题

ぃ、小莉子 提交于 2019-12-27 06:52:16
B. Bear and Friendship Condition 题目连接: http://codeforces.com/contest/791/problem/B Description Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also

Educational Codeforces Round 42 (Rated for Div. 2)

て烟熏妆下的殇ゞ 提交于 2019-12-27 06:20:28
                                        A. Equator Polycarp has created his own training plan to prepare for the programming contests. He will train for n n days, all days are numbered from 1 1 to n n, beginning from the first. On the i i-th day Polycarp will necessarily solve a i ai problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems. Determine the index of day when Polycarp will celebrate the equator. Input The

Educational Codeforces Round 64 (Rated for Div. 2)题解

不打扰是莪最后的温柔 提交于 2019-12-27 04:20:54
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人。需要注意以下就是正方形、圆以及三角形的情况,它们在上面的顶点是重合的。 其余的参照样例判断一下就好了了。 具体证明我也不会 代码如下: Code #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 5; int n; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0) ; cin >> n; for(int i = 1; i <= n; i++) cin >> a[i] ; int ans = 0; int flag = 0; for(int i = 2; i <= n; i++) { if(a[i] + a[i - 1] == 5 || a[i] == a[i - 1]) flag = 1; } if(flag) cout << "Infinite" ; else { cout << "Finite" << '\n'; for(int i = 2; i <= n; i++) { if(a[i - 1] ==

Educational Codeforces Round 44 (Rated for Div. 2)

*爱你&永不变心* 提交于 2019-12-23 13:36:25
题目链接: https://codeforces.com/contest/985 ’A. Chess Placing 题意 :给了一维的一个棋盘,共有n(n必为偶数)个格子。棋盘上是黑白相间的。现在棋盘上有n/2个棋子,让你全部移动到黑色格子或者白色格子,要求步数最少,并输出步数。 题解:由于黑色或者白色都是固定位置,我们对棋子位置排个序,然后全部移动到任意一个颜色,取两个最小步数的最小值就好了。 1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define mod 1000000007 5 #define INF 0x3f3f3f3f 6 #define LL long long 7 #define pb push_back 8 #define pbk pop_back 9 #define ls(i) (i<<1) 10 #define rs(i) (i<<1|1) 11 #define mp make_pair 12 using namespace std; 13 const int N=1e5+10; 14 int a[N],p[N]; 15 int n,d,ans1,ans2; 16 int main()