USACO 1.2 sections

Source: Internet
Author: User

PROB Milking Cows [ANALYSIS] Method for Interval Problems

// Maximum continuous interval. /* Consider: when the points at both ends of the interval are equal. * How to sort intervals * When the value required for each interval changes, take max * each time, consider the initial and last intervals */struct cmp {bool operator () (PII, PII B) {if (. first = B. first) return. second> B. second; // increment return. first> B. first; }}; int n; priority_queue <PII, vector <PII>, cmp> Q; int main () {// FOPENFOPENTIFOPENTOint a, B, sum = 0, maxx = 0, maxf = 0; SCF (n); F (I, n) {SCFD (a, B); Q. push (make_pair (a, B);} PII tmp; int f, t; tmp = Q. top (); Q. pop (); f = tmp. first; t = tmp. second; s Um + = t-f; maxx = max (maxx, sum); while (! Q. empty () {tmp = Q. top (); Q. pop (); // DB (tmp. first <"" <tmp. second); if (t> = tmp. first) {if (t> = tmp. second) {continue;} else {sum + = tmp. second-t; maxx = max (maxx, sum); t = tmp. second ;}} else {maxx = max (maxx, sum); maxf = max (maxf, tmp. first-t); f = tmp. first; t = tmp. second; sum = t-f; maxx = max (maxx, sum) ;}} printf ("% d \ n", maxx, maxf );}

PROB Transformations [ANALYSIS] --- this question is really speechless and requires a lot of things. For example, you must follow steps 1-7. (The program cannot have a return statement, or it will force exit. I don't know why .)

Evaluate the knowledge of basic matrix transformation.

// Basic operations on the matrix:
/* Rotate
* Image
*/

// Pre-> change; void way_1 (int n, char matC [] [MAXN], char matP [] [MAXN]) {// 90 ° F (I, n) clockwise) F (j, n) {matC [j] [n-i-1] = matP [I] [j] ;}// image void way_4 (int n, char matC [] [MAXN], char matP [] [MAXN]) {F (I, n) F (j, n) {matC [I] [n-j-1] = matP [I] [j];}

The answer is concise enough!

 

PROB Name That Number [ANALYSIS] ---- hash_map + dfs enumerative has been sorted out. WA once, because none is not +, and the question is not taken seriously!

There are two ways to do this problem. one is, given the number, to generate all possible strings that encode to that number and look them up in the dictionary. since there are 3 letters for each number and 12 digits in the string, that's 312 = 531441 lookups into a dictionary of size 5000, which although manageable wocould be a little on the long side (binary search can help this ).

There are two ways to do this. One is to give a number to identify the possible strings, and then go to dictionary to find whether the strings exist. Because each character has a total of 12 characters, the total number of strings is 312 = 531441, And the dictionary size is 5000, however, the time will be long (Binary Search can help you ). (The maximum time of hash_map used is 140 MS .) (I submitted both answers and found that they are all 0.000 s ,--! Very powerful mark!

Technorati Tag: mark powerful

)

Or, we can examine each word in the dictionary to see if it maps to the digits of the number in question. this has the advantage of a shorter program that probably will work right first time.

Alternatively, we can also query each word in a dictionary to see if it is mapped to a certain number in the query. The advantage of this fast program may lead him to the first place! (The translation here is not good .)

struct str_hash {size_t operator () (const string & str) const {unsigned long __h =0;for(size_t i =0;i<str.size();i++)__h =5 * __h + str[i];return size_t(__h);}};hash_map<string,int,str_hash> hsi;map<string,int> mpsi;char hsh[10][3]={{'\0','\0','\0'},{'\0','\0','\0'},{'A','B','C'},{'D','E','F'},{'G','H','I'},{'J','K','L'},{'M','N','O'},{'P','R','S'},{'T','U','V'},{'W','X','Y'}};char ss[MAXNT],sst[MAXNT];int len,flag=0;void dfs(int x){if(x == len) {sst[x]='\0';if(hsi.find(sst) != hsi.end()) {flag=1;puts(sst);}return;}for(int i = 0;i<3;i++){sst[x] = hsh[ss[x]-'0'][i];dfs(x + 1);}}int main() {FOPENTIFOPENTOFILE *fin  = fopen ("dict.txt", "r");/*      2: A,B,C     5: J,K,L    8: T,U,V *           3: D,E,F     6: M,N,O    9: W,X,Y *           4: G,H,I     7: P,R,S */while(~fscanf(fin," %s",ss)) {hsi[ss] = 1;}//DB(hsi.size())while(~SCFS(ss)){flag =0;len = strlen(ss);dfs(0);if(!flag) puts("NONE");}}

 

PROB Palindromic Squares [ANALYSIS] ----- hexadecimal conversion + judgment of palindromic

This time it is almost the same as analysis.

char ss[MAXN];int kk=0;void fun(int n,int r){        if(n) {                fun(n/r,r);                ss[kk++] = (n%r>9? n%r-10+'A':n%r+'0');        }}bool is_palind(char ss[]){int len = strlen(ss);int i = 0,j = len-1;bool flag = 1;while(i < j) if(ss[i++] != ss[j--]) flag = 0;return flag;}int main(){FOPENTIFOPENTOint b;SCF(b);for(int i = 1;i<=300;i++) {fun(i * i,b);ss[kk] = '\0';kk=0;if(is_palind(ss)) {fun(i,b);ss[kk] = '\0';printf("%s ",ss);kk=0;fun(i*i,b);ss[kk] = '\0';puts(ss);kk=0;}}}

 

PROB Dual Palindromes [ANALYSIS] ---- hexadecimal conversion + judgment + process control... You can type a table and forget it.

char ss[MAXN];int kk=0;void fun(int n,int r){        if(n) {                fun(n/r,r);                ss[kk++] = (n%r>9? n%r-10+'A':n%r+'0');        }}void transi_(int n,int b){memset(ss,'\0',sizeof(ss));kk=0;fun(n,b);}bool is_palind(char ss[]){int len = strlen(ss);int i = 0,j = len-1;bool flag = 1;while(i < j) if(ss[i++] != ss[j--]) flag = 0;return flag;}int main(){FOPENTIFOPENTOint s,n,k=0,flag;while(~SCFD(s,n)){k=0;n++;while(k != s) {//DB(n)flag = 0;FOR(i,2,10) {transi_(n,i);if(is_palind(ss)){flag++;}if(flag == 2) break;}if(flag == 2) {PCFLN(n);k++;}n++;}}}

1.2 Summary:

  • Read the questions carefully, or trick will not be able to find out.
  • Reverse thinking. The third question is an example.
  • Do not forget to create a table.
  • Think carefully about the question and summarize your methods.

It is expected that the customs clearance will be completed after a period of time.

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.