【NOIp】NOIp2008

NOIp2008

T1 stupid monkey

Tags: STL

Letters with a stored digital map (the number of occurrences) mapping

Since the data is very small, it can not be directly linear sieve $ {\ sqrt {n}} $ to

code

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 namespace gengyf{
 4     inline int read(){
 5         int x=0,f=1;char s=getchar();
 6         while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
 7         while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
 8         return x*f;
 9     }
10     int maxx,minn=999;char word[101];
11     map<char,int>m;
12     bool prime(int x){
13         for(int i=2;i<=sqrt(x);i++){
14             if(x%i==0){
15                 return 0;
16             }
17         }
18         return 1;
19     }
20     int main(){
21         cin>>word;
22         int l=strlen(word);
23         for(int i=0;i<l;i++){
24             m[word[i]]++;
25             maxx=max(maxx,m[word[i]]);
26         }
27         for(int i=0;i<l;i++){
28             minn=min(minn,m[word[i]]);
29         }
30         if(maxx==0||maxx==1){
31             printf("No Answer\n");
32             return 0;
33         }
34         if(maxx-minn==1||maxx-minn==0){
35             printf("No Answer\n0");
36             return 0;
37         }
38         if(prime(maxx-minn)){
39             printf("Lucky Word\n");
40             printf("%d\n",maxx-minn);
41         }
42         else {
43             printf("No Answer\n0");
44         }
45         return 0;
46     }
47 }
48 int main(){
49     gengyf::main();
50     return 0;
51 }
T1

 I thought I was a stupid monkey, minn forget an initial value WA, WA has no special sentenced to 1 once, too zz the

T2 Equation matchstick

Tags: No Tags

Direct enumeration i, j from 1 to a maximum value, if the composition i, j, i + j is the total number of matchstick exactly equal to n-4 Statistics answer +1

This question is the only say is how to find the maximum value because the maximum value is not known, so the enumeration range is slightly larger opening (1e3,1e4 this

N is then run again from 1 to 24 to record the i, j is the maximum maxx, maxx then replace just the maximum range

code

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 namespace gengyf{
 4     inline int read(){
 5         int x=0,f=1;char s=getchar();
 6         while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
 7         while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
 8         return x*f;
 9     }
10     int n,s[10]={6,2,5,5,4,5,6,3,7,6},ans;
11     int match(int x){
12         int tmp=0;
13         if(x<10)return s[x];
14         while(x!=0){
15             int xx=x%10;
16             tmp+=s[xx];
17             x/=10;
18         }
19         return tmp;
20     }
21     int main(){
22         n=read();
23         for(int i=0;i<=999;i++)
24             for(int j=0;j<=999;j++){
25                 int k=i+j;
26                 if(match(i)+match(j)+match(k)==n-4){
27                     ans++;
28                 }
29             }
30         printf("%d",ans);
31         return 0;
32     }
33 }
34 int main(){
35     gengyf::main();
36     return 0;
37 }
T2

Seeking maximum code

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 namespace gengyf{
 4     inline int read(){
 5         int x=0,f=1;char s=getchar();
 6         while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
 7         while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
 8         return x*f;
 9     }
10     int n,s[10]={6,2,5,5,4,5,6,3,7,6},ans,maxx=0;
11     int match(int x){
12         int tmp=0;
13         if(x<10)return s[x];
14         while(x!=0){
15             int xx=x%10;
16             tmp+=s[xx];
17             x/=10;
18         }
19         return tmp;
20     }
21     int main(){
22         n=read();//输入n为0
23         while(n!=25){
24             n++;
25             for(int i=0;i<=999;i++)
26                 for(int j=0;j<=999;j++){
27                     int k=i+j;
28                     if(match(i)+match(j)+match(k)==n-4){
29                         maxx=max(maxx,max(i,j));
30                         ans++;
31                     }
32                 }
33         }
34         printf("%d",maxx);
35         return 0;
36     }
37 }
38 int main(){
39     gengyf::main();
40     return 0;
41 }
Max

Attached: Search problem solution

T3 pass notes

Tags: dp

The two left-handed down the note to see Chengdu

Compare and wanted to transfer 3-dimensional

Only four cases:

From the top to the first, second from the top to

First come from the top, second from the left to

The first from the left, the second from the top to

The first from the left, the second from the left to

f [i] [j] [k] represents the horizontal and vertical coordinates of the current position is the sum of i, the ordinate is the first j, the second ordinate k

So equation

From the top to the first, second from the top to f (i, j, k) = max {f (i, j, k), f (i-1, j-1, k-1) + a [ j] [ij] + a [k] [ik]}

From the top to the first, second from the left to f (i, j, k) = max {f (i, j, k), f (i-1, j-1, k) + a [j] [ij] + a [k] [ik]}

First from the left, the second from the top to f (i, j, k) = max {f (i, j, k), f (i-1, j, k-1) + a [j] [ij] + a [k] [ik]}

First from the left, the second from the left to f (i, j, k) = max {f (i, j, k), f (i-1, j, k) + a [j] [ij ] + a [k] [ik]}

but! It is not good enough!

We use the scroll array

I can be found only transferred from i-1 over the first dimension can be omitted

j, k are transferred from the smaller j, k, so will j, k enumeration prevent reverse is modified prior to transfer

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 int f[201][201],a[201][201];
 5 int maxx(int a,int b,int c,int d){
 6     if(a<b)a=b;
 7     if(c>a)a=c;
 8     if(d>a)a=d;
 9     return a;
10 }
11 int main(){
12     int n,m;
13     scanf("%d%d",&n,&m);
14     for(int i=1;i<=n;i++)
15       for(int j=1;j<=m;j++){
16           scanf("%d",&a[i][j]);
17       }
18     f[1][2]=a[1][2]+a[2][1];
19     for(int k=4;k<n+m;++k)
20       for(int i=n;i>0;--i)
21         for(int j=n;j>i;--j){
22             f[i][j]=maxx(f[i][j],f[i-1][j-1],f[i-1][j],f[i][j-1])+a[i][k-i]+a[j][k-j];
23             if(i==j)f[i][j]-=a[i][k-i];
24         }
25     int ans=0;
26     for(int i=1;i<=n;i++)
27       for(int j=1;j<=n;j++){
28           ans=max(f[i][j],ans);
29       }
30     printf("%d\n",f[n-1][n]);
31     return 0;
32 }
T3

Early code ~

Attachment: large classrooms passed a note (data enhanced version)

T4 dual-stack Sort

Tags: graph theory, greedy, dp

Dp start and again in the same judgment that can not be the stack

We have found that if the three positions i, j, k satisfy i <j <k, and a [k] <a [i ] <a [j] is not feasible apparent

Then can not coexist i, j even edge, a bipartite graph coloring, if the dye is not feasible solution exists DESCRIPTION

And because the lexicographically minimum requirements, so try to put S1

code

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 namespace gengyf{
 4     inline int read(){
 5         int x=0,f=1;char s=getchar();
 6         while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
 7         while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
 8         return x*f;
 9     }
10     int f[1010],n,a[1010],color[1010];
11     struct edge{
12         int nxt,to;
13     }e[1010];
14     int head[1010],cnt,s1[1010],s2[1010];
15     void add(int from,int to){
16         e[++cnt].to=to;e[cnt].nxt=head[from];
17         head[from]=cnt;
18     }
19     bool dfs(int u,int c){
20         color[u]=c;
21         for(int i=head[u];i;i=e[i].nxt){
22             int to=e[i].to;
23             if(color[to]==color[u])return false;
24             if(!color[to]&&!dfs(to,3-c))return false;
25         }
26         return true;
27     }
28     int main(){
29         n=read();
30         for(int i=1;i<=n;i++){
31             a[i]=read();
32         }
33         f[n]=a[n];
34         for(int i=n-1;i>=1;i--){
35             f[i]=min(f[i+1],a[i]);
36         }
37         for(int i=1;i<=n;i++)
38             for(int j=i+1;j<=n;j++){
39                 if(a[i]<a[j]&&f[j]<a[i]){
40                     add(i,j);add(j,i);
41                 }
42             }
43         for(int i=1;i<=n;i++){
44             if(!color[i]&&!dfs(i,1)){
45                 printf("0");
46                 return 0;
47             }
48         }
49         int c1=0,c2=0,now=1;
50         for(int i=1;i<=n;i++){
51             if(color[i]==1){
52                 s1[++c1]=a[i];printf("a ");
53             }
54             else {
55                 s2[++c2]=a[i];printf("c ");
56             }
57             while(s1[c1]==now||s2[c2]==now){
58                 if(s1[c1]==now){
59                     printf("b ");c1--;
60                 }
61                 else {
62                     printf("d ");c2--;
63                 }
64                 now++;
65             }
66         }
67         return 0;
68     }
69 }
70 int main(){
71     gengyf::main();
72     return 0;
73 }
T4

 

这题看完之后一脸懵,想不到什么正经方法,苦死无果后看题解才恍然大悟,这告诉我们不仅要知道某一算法的应用范围,还要理解算法本质QAQ

 


 

 

 

Guess you like

Origin www.cnblogs.com/gengyf/p/11455617.html