Codeforces Round #397 by Kaspersky Lab and Barcelona bootcamp (div. 1 + div. 2 combined)

Source: Internet
Author: User

Good luck, divided into the room I first began hack C, hack 12, I heard that f sand carving MO team but I will not, the last less than 15 minutes to come up with an e-question method hit a wave of results hanging, and finally although the total is a little reluctant.

A.neverending Competitions

The main idea: someone flew out of the house and flew back to fly out again to fly back again ..., now give you N Zhang he used ticket (n<=100), to ensure legal but order upset, ask him now home or outside.

Idea: n%2 problem ... did not see the topic to ensure that the field must fly home, write a judge the degree of access.

#include <cstdio>inlineintRead () {intx=0;CharC;  while((C=getchar ()) <'0'|| C>'9');  for(; c>='0'&&c<='9'; C=getchar ()) x= (x<<3) + (x<<1) +c-'0'; returnx;}intMain () {intN=read (), x,a=0;Chars[Ten]; scanf ("%s", s); X= (s[0]* ++s[1])* ++s[2];  while(n--) {scanf ("%s", s); if((s[0]* ++s[1])* ++s[2]==X) + +A; if((s[5]* ++s[6])* ++s[7]==X)--A; } puts (a?"Contest":"Home");}

B.code Obfuscation

The main topic: An article, met the first never touched the word to replace all of this word to a, the second is replaced by B, to a length of not more than 500 lowercase letter string, asked whether it may be an article encryption obtained.

Idea: Take a variable to use which letter ... Hand speed problem

#include <cstdio>inlineintRead () {intx=0;CharC;  while((C=getchar ()) <'0'|| C>'9');  for(; c>='0'&&c<='9'; C=getchar ()) x= (x<<3) + (x<<1) +c-'0'; returnx;}#defineMN 500Chars[mn+5];intMain () {inti,x='a'; scanf ("%s", s);  for(i=0; s[i];++i) {if(s[i]>x)return 0*puts ("NO"); if(s[i]==x) + +x; } puts ("YES");}

C.table Tennis Game 2

The main topic: Give A,b,k, Ask (A, b) is broken up to a few (k,x) or (x,k) (x<k) and, if no solution output-1.

Idea: Determine if there is a solution and then output a/k+b/k on the line, how to judge the solution has become the biggest hack point in the game, the right way Is (a<k&&b%k!=0) | | (b<k&&a%k!=0) There is no solution, the wrong approach has a/k+b/k>0, a<k and b<k without solution.

#include <cstdio>#include<algorithm>using namespaceStd;inlineintRead () {intx=0;CharC;  while((C=getchar ()) <'0'|| C>'9');  for(; c>='0'&&c<='9'; C=getchar ()) x= (x<<3) + (x<<1) +c-'0'; returnx;}intMain () {intK,a,b,ans; K=read (); A=read (); b=read (); if(a<k&&b%k)return 0*puts ("-1"); if(b<k&&a%k)return 0*puts ("-1"); printf ("%d", a/k+b/k);}

D.artsem and Saunders

Title: n<=100,000, give [1,n] to [1,n] mapping F (x), to [1,n] to [1,m] mapping g (x) and [1,m] to [1,n] mapping H (x), so that any x belongs to [1,m]g (H (x)) =x, any x belongs to [1,n ]h (g (x)) =f (x).

Idea: The observation found that for each group of equal F (x) x, there must be a satisfying x=f (x), all of which map to [1,m] one mapping to f (x) on the line.

#include <cstdio>inlineintRead () {intx=0;CharC;  while((C=getchar ()) <'0'|| C>'9');  for(; c>='0'&&c<='9'; C=getchar ()) x= (x<<3) + (x<<1) +c-'0'; returnx;}#defineMN 100000inta[mn+5],c[mn+5],p[mn+5],PN;intMain () {intn=read (), I;  for(i=1; i<=n;++i)if((A[i]=read ()) ==i) p[c[i]=++pn]=i;  for(i=1; i<=n;++i)if(!c[a[i]])return 0*puts ("-1"); printf ("%d\n", PN);  for(i=1; i<=n;++i) printf ("%d", C[a[i]]);p UTS ("");  for(i=1; i<=pn;++i) printf ("%d", P[i]);}

E.tree Folding

The main topic: a tree, choose a point each time, connect with him two of the same long chain and into one (chain can not be connected to other places), ask the last can be the tree and into a chain, can find the shortest chain length.

Thought: I blind yy a practice, will not prove, please feel free to know. Find each point as the root of his son's subtree depth, if the depth of the number of more than 2 no solution, if all are not more than 2 the answer for each point is the root of the two depth of the sum of the minimum value, the depth can be DP O (n) to find the case of the root of 1 and O (1) Move the root

#include <cstdio>#include<algorithm>using namespaceStd;inlineintRead () {intXCharC;  while((C=getchar ()) <'0'|| C>'9');  for(x=c-'0';(C=getchar ()) >='0'&&c<='9';) x= (x<<3) + (x<<1) +c-'0'; returnx;}#defineMN 200000structedge{intNx,t;} e[mn*2+5];inth[mn+5],en,d[mn+5],d1[mn+5],d2[mn+5],c[mn+5],g;inlinevoidInsintXinty) {e[++en]= (Edge) {h[x],y};h[x]=en; e[++en]= (Edge) {h[y],x};h[y]=en;}voidCalintXintd) {    if(!d1[x]) D1[x]=d;Else if(d1[x]!=d)if(!d2[x]) D2[x]=d;Else if(D2[x]!=d) g=1;}voidPreintXintf) {     for(intI=H[X];I;I=E[I].NX)if(e[i].t!=f) {pre (E[I].T,X); if(D[e[i].t]>=d[x]) c[x]=d[x],d[x]=d[e[i].t]; Else if(D[e[i].t]>c[x]) c[x]=d[e[i].t];    Cal (x,d[e[i].t]); }    ++d[x];++c[x];}voidDfsintXintFintFD) {    if(f) Cal (X,FD);  for(intI=H[X];I;I=E[I].NX)if(e[i].t!=f) Dfs (E[i].t,x,max (FD+1, d[x]==d[e[i].t]+1?c[x]:d [x]));}intMain () {intn=read (), I,ans;  for(i=1; i<n;++i) Ins (read (), read ()); Pre (1,0);d FS (1,0,0); if(g)return 0*puts ("-1");  for(ans=d1[1]+d2[1],i=2; i<=n;++i) Ans=min (ans,d1[i]+D2[i]);  while(ans%2==0) ans/=2; printf ("%d", ans);}

Codeforces Round #397 by Kaspersky Lab and Barcelona bootcamp (div. 1 + div. 2 combined)

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.