Codeforces Round #384 Div.2

Source: Internet
Author: User
Tags cmath

A:vladik and flights

Topic: Given a length of n 01 string and a, b two positions, if one position and the other position on the same number, then the two locations between the cost of reaching each other is 0, otherwise the cost is the distance between the two locations, ask the minimum cost from a to a.

Idea: The answer is not 0 that 1,0 is the same as the number of the two positions, 1 means that there must be 0 and 1 at the same time, then the two points are moved to 0, 1, the location of the junction, then the answer is 1.

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm>using namespace std; #define MAXN 100005const int inf=1e9;int n,st,ed,ans;char s[maxn];inline int read () {int X=0,f=1;char Ch=getchar (); for (;ch< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()) if (ch== '-') f=-1;for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x* F;} inline void print (int x) {if (x>=10) print (X/10);p Utchar (x%10+ ' 0 ');} int main () {N=read (), St=read (), Ed=read (), scanf ("%s", s+1);p UTS (s[st]==s[ed]? " 0 ":" 1 "); return 0;}

B:chloe and the sequence

The main idea: given a sequence of construction, the first sequence is [1], the second sequence is [1,2,1], the third sequence is [1,2,1,3,1,2,1], then the nth sequence is to fold the n-1 sequence and then in the middle of a number n, and then ask the nth sequence of what is the number of K.

Idea: Set Len to the length of the nth sequence (apparently len= (1<<n)-1), Mid for the position of the most intermediate number, and then if K>mid, apparently a[k]=a[len-k+1] (satisfying symmetry), and then if K<mid, Then we can think of the sequence binary as a sub-problem, and the output of this is the first sequence of K==mid.

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm>using namespace Std;int n;long long len,k;inline Long Long read () {Long long X=0,f=1;char ch=getchar (); for (; c h< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()) if (ch== '-') f=-1;for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x* F;} inline void print (int x) {if (x>=10) print (X/10);p Utchar (x%10+ ' 0 ');} int main () {N=read (), K=read (); len= (1ll<<n) -1;for (int i=n;i;i--) {if (k==len/2+1) {printf ("%d\n", I); return 0;} if (k>len/2+1) k=len-k+1;len>>=1;} return 0;}

C:vladik and Fractions

Title: Given n, find three different integers to make 2/n=1/a+1/b+1/c.

Idea: 2/n=1/n+1/(n+1) +1/(n (n+1)), pay attention to each other to special n=1 case.

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm>using namespace Std;int n,m;inline int read () {int X=0,f=1;char ch=getchar (); for (;ch< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()) if (ch== '-') f=-1;for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x* F;} inline void print (int x) {if (x>=10) print (X/10);p Utchar (x%10+ ' 0 ');} int main () {n=read (); if (n==1) {puts ("-1"); return 0;} else{printf ("%d%d%d\n", n,n+1,n* (n+1));} return 0;}

D:chloe and Pleasant Prizes

The main idea: a tree with 1 roots, given the point right of each point, find two sub-trees that do not intersect and make their weights and as large as possible, if the output-1 is not found.

Idea: The first thing that can not be found is the case of a chain (of course, 1 must be an endpoint of the chain), and then the answer directly on the tree DP, and trees DP for the longest chain of the basic hair.

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm>using namespace std; #define MAXN 200005const Long long inf=1e18;int n,m,maxdeg,tot;long long Ans;int Now[max N],pre[maxn*2],son[maxn*2],deg[maxn];long long sum[maxn];bool vis[maxn];inline int read () {int X=0,f=1;char Ch=getchar (); for (;ch< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()) if (ch== '-') f=-1;for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x* F;} inline void print (int x) {if (x>=10) print (X/10);p Utchar (x%10+ ' 0 ');} void Add (int a,int b) {Son[++tot]=b;pre[tot]=now[a];now[a]=tot;} void link (int a,int b) {Add (a), add (B,a);} Long long tree_dp (int x,int fa) {long long mx=-inf,tx=-inf;for (int p=now[x];p; p=pre[p]) if (SON[P]!=FA) {Long Long tmp=tree _DP (son[p],x); if (TMP&GT;MX) Tx=mx,mx=tmp;else Tx=max (tx,tmp); Ans=max (ANS,TX+MX); sum[x]+=sum[son[p]];} Return Max (mx,sum[x]);} void Dfs (int x,int fa) {vis[x]=1;for (int p=now[x];p; p=pre[p]) if (SON[P]!=FA) {DFS (son[p],x); break;}}BOOL Check () {DFS (1,0); for (int i=1;i<=n;i++) if (!vis[i]) return 1;return 0;} int main () {n=read (); ans=-inf;for (int i=1;i<=n;i++) sum[i]=read (); for (int i=1,a,b;i<n;i++) A=read (), B=read (), Link (A, b), if (!check ()) {puts ("impossible"); return 0;} TREE_DP (1,0);p rintf ("%lld\n", ans); return 0;}

E:vladik and Cards

Title: In a given sequence consisting of only 1 to 8, a subsequence is found that satisfies the following conditions: 1. Any number of occurrences of the number of times can not be more than 1, that is, each number appears x times, or appear x+1 times. 2. If the first occurrence of a number in a subsequence, it must all appear, that is, the same number in the subsequence must be a line segment and cannot be broken. On the basis of the above, it is required that the subsequence length is longest.

Idea: The pressure DP is obviously, the key is the dichotomy is difficult to think, because a number is either x times, or appear x+1 times, then we have two points this X (obviously monotonic), and then consider how to check the good.

Because the answer must be x*y+ (8-y) * (x+1), this y is how many numbers appear x times, then you can make the state F[i][s] represents the current state is S, the current is at the second of the value of Y, and then enumerate which number is currently selected to transfer.

#include <cmath> #include <cstdio> #include <vector> #include <cstring> #include <iostream > #include <algorithm>using namespace std; #define MAXN 1005#define MAXS (1<<8) const int Inf=1e9;int n,ans= -inf;int a[maxn],cnt[9];int f[maxn][maxs];inline int read () {int X=0,f=1;char ch=getchar (); for (;ch< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()) if (ch== '-') f=-1;for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x=x*10+ch-' 0 '; return x* F;} Vector<int> v[9];void Clear () {memset (cnt,0,sizeof (CNT)), memset (F,-1,sizeof (f));} BOOL Check (int mid) {clear (); f[1][0]=0;for (int i=1;i<=n;i++) {for (int s=0; s<maxs; s++) {if (f[i][s]==-1) continue;for (int j=1;j<=8;j++) if (! ( s& (1<< (j-1))) {int x=cnt[j]+mid;if (x>v[j].size ()) continue;f[v[j][x-1]+1][s| ( 1<< (j-1))]=max (f[v[j][x-1]+1][s| ( 1<< (j-1))],f[i][s]), if (++x>v[j].size ()) continue;f[v[j][x-1]+1][s| ( 1<< (j-1))]=max (f[v[j][x-1]+1][s| ( 1<< (j-1))],f[i][s]+1);}} cnt[a[i]]++;} int tt=-1;for (int i=1;i<=n+1;i++) Tt=max (tt,f[i][maxs-1]); return tt==-1?0: (Ans=max (ans,tt*) + (mid+1) 8-tt), 1);} int main () {n=read (); for (int i=1;i<=n;i++) A[i]=read (), V[a[i]].push_back (i); int L=1,r=n;while (l<=r) {int mid= (l +r) >>1;if (check (mid)) L=mid+1;else R=mid-1;} if (Ans==-inf) {ans=0;for (int i=1;i<=8;i++) if (V[i].size ()) ans++;} printf ("%d\n", ans); return 0;}

  

Codeforces Round #384 Div.2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.