Topcoder SRM 639 (Div.2)

Source: Internet
Author: User

A.electronicpeteasy

"Test Instructions" a number st1 start, each add P1, altogether add T1 times, another number St2 start, each add P2, altogether add T2 times, the number of inputs are less than 1000, asked whether the two number is not possible equal, it is possible to output "easy", otherwise output "difficult"

"Explanation" clearly lists two loops, 1000*1000 will not time out

Const string dif= "difficult"; const string eas= "Easy"; class electronicpeteasy{public:string Isdifficult (int st1, int p1, int t1, int st2, int p2, int t2) {int i,j,k;int m1=st1+p1* (t1-1), int m2=st2+p2* (t2-1), for (I=ST1;I<=M1;I+=P1) {for (j=st2 ; j<=m2;j+=p2) {if (i==j) return dif;}} return EAS;}};
B.alicegameeasy

"Test Instructions" from 1 to N, Alice took out a few numbers accumulated for X, the people take out a few number accumulation for y, give x and Y, ask Alice to do less to get how many number (impossible when output-1).

"Explain" greedy algorithm

First Judge x+y=n* (n+1)/2, can find an integer n, can not be found out or to find the n is not an integer so output-1 just fine

Now that we have N, let Alice take it from the largest number, n,n-1,n-2 ... Until k is present, so that just n+ (n-1) + (n-1) +...+ (n-k+1) >=x ("Just" means n+ (n-1) + (n-1) +...+ (n-k) <x), the answer is K.

Class Alicegameeasy{public:long Long Findminimumvalue (long long x, long long y) {LL i,j,k; LL dt= (x+y) *8+1; ll sqr_dt= (LL) (sqrt (DT)); if (SQR_DT*SQR_DT!=DT) return-1;sqr_dt--;if (sqr_dt%2) return-1; LL N=SQR_DT/2; LL Ans=0;while (x>0) {x-=n;n--;ans++;} return ans;};
C.boardfoldingdiv2

"Test Instructions" gives a 01 matrix (up to 50*50) (that is, the title of the paper), now to fold the matrix, folding to meet:

1. Folding parallel to an edge

2. To collapse between adjacent two columns of numbers

3. The number below the fold is equal to the number above

Now ask how many kinds of folding methods, (which are the same method if the same matrix interval is folded)

The "explanation" is clearly the ability to separate rows from columns, ANSR only the number of methods that are collapsed by rows, and ANSC only by the number of methods that are collapsed by the column, and finally ANSR*ANSC is the answer.

Because the method is the same by row and column, the following example is listed below.

The topic of the person is very conscientious, only to the scale of the 50*50, and the most important is only 0 and 1, then each column of the 01 string as a binary number, with a long long can be stored completely, we stored in the c[] array.

General idea : Enumerate the right boundary, if the right boundary is feasible (need preprocessing), then enumerate the left boundary, if the left boundary is feasible, even if it is a solution.

This is preprocessing: set Right[i]=1 to fold from right, can be folded to I, that is, after folding the number of i+1~n-1 (number is 0 to n-1) all disappeared. Of course, the right[i]=0 indicates that it cannot be folded to the number I.

Then enumerate left bounds, right[k]=1, enumerate left bounds I, with left[i] to start from left to collapse, can be folded to I (and right[] is the same idea, just a change of direction).

The algorithm is O (n3)

typedef long Long Ll;int n,m; LL r[55],c[55];int ansr,ansc,lef[55],rig[55];class boardfoldingdiv2{public:int howmany (vector <string> paper) { int I,j,k;memset (r,0,sizeof (R)); Memset (C,0,sizeof (c)); Ansr=ansc=0;n=paper.size (); M=paper[0].size (); for (i=0;i <n;i++) {for (j=0;j<m;j++) {r[i]=r[i]*2+paper[i][j]-' 0 '; c[j]=c[j]*2+paper[i][j]-' 0 ';}} /* For each rampage operation */memset (rig,0,sizeof (rig)); Rig[n-1]=1;for (i=n-2;i>=0;i--) {for (j=1;i+j<n&&i-j+1>=0;j + +) {if (r[i+j]==r[i-j+1]) {if (Rig[i+j]) {rig[i]=1;break;}} else break;}} for (k=n-1;k>=0;k--)//Enumerate right bounded {if (rig[k]==0) continue;//If right bounds are unreachable, Continuememset (lef,0,sizeof (lef)); lef[0]=1; ansr++ for (i=1;i<=k;i++)//Enumerate left bounds {for (j=1;i+j-1<=k&&i-j>=0;j++)//Determine if the left bounds can be up to {if (R[i+j-1]==r[i-j]) {if (lef[ I-J])//If LEFT[I-J] can reach and just can be along the number I and the number of i-1 folding {lef[i]=1;//set to jump out of the break;}} else break;} if (lef[i]!=0) ansr++;}} /* For each column operation */memset (rig,0,sizeof (rig)); Rig[m-1]=1;for (i=m-2;i>=0;i--) {for (j=1;i+j<m&&i-j+1>=0;j+ +) {if (c[i+j]==c[i-j+1]) {if (Rig[i+j]) {rig[i]=1;break;}} else break;}} for (k=m-1;k>=0;k--) {if (rig[k]==0) Continue;memset (lef,0,sizeof (lef)), lef[0]=1; Ansc+=min (1,lef[0]*rig[k]); for (i=1;i<=k;i++) {for (j=1;i+j-1<=k&&i-j>=0;j++) {if (C[i+j-1]==c[i-j]) {if (Lef[i-j]) {lef[i]=1;break;}} else break;} if (lef[i]!=0) ansc++;}} return ANSR*ANSC;};

Topcoder SRM 639 (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.