2015.07.30 (4)

1001 Olympiad

写的时候直接统计的个数,后来才知道应该递推一下

 1 #include <cstdio>
 2 #include <ctime>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <stack>
10 #include <queue>
11 #include <string>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 #define getmid(l,r) ((l) + ((r) - (l)) / 2)
17 #define MP(a,b) make_pair(a,b)
18 #define PB push_back
19 
20 typedef long long ll;
21 typedef pair<int,int> pii;
22 const double eps = 1e-8;
23 const int INF = (1 << 30) - 1;
24 const int maxn = 100005;
25 int f[maxn];
26 
27 int vis[maxn];
28 
29 int ok(int x){
30     int a[10];
31     int cnt = 0;
32     while(x){
33         a[cnt++] = x%10;
34         x = x/10;
35     }
36     int b[10];
37     memset(b,0,sizeof(b));
38     for(int i = 0;i < cnt;i++){
39         if(!b[a[i]]) b[a[i]] = 1;
40         else return 0;
41     }
42 
43     return 1;
44 }
45 
46 int main(){
47     memset(vis,0,sizeof(vis));
48     for(int i = 1;i <= maxn;i++) if(ok(i)) vis[i] = 1;
49 //    for(int i = 1;i <= 100;i++) printf("vis[%d] = %d
",i,vis[i]);
50     f[0] = 1;
51     for(int i = 1;i <= maxn;i++) {
52         if(ok(i)) f[i] = f[i-1] +1;
53         else f[i] = f[i-1];
54     }
55     
56 
57    int T;
58    scanf("%d",&T);
59    while(T--){
60        int a,b;
61        scanf("%d%d",&a,&b);
62        printf("%d
",f[b] - f[a-1]);
63    }
64    return 0;
65 }
View Code

1002 Problem Killer

可以尺取法,或者递推

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=1000005;
17 
18 LL  a[maxn];
19 int n;
20 
21 int main(){
22     int T;
23     scanf("%d",&T);
24     while(T--){
25         scanf("%d",&n);
26         for(int i = 0;i < n;i++) scanf("%I64d",&a[i]);
27         
28         if(n <= 2){
29             printf("%d
",n);
30             continue;
31         }
32         
33         int res = 0,ans = 0;
34         for(int i = 0;i < n;){
35             int j = i+1;
36             while(j+1 < n && a[j-1] + a[j+1] == 2*a[j]) j++;
37             res = max(res,j-i+1);
38             i = j;
39         //    printf("j = %d  res = %d
",j,res);
40         }
41         
42         for(int i = 0;i < n;){
43             int j = i+1;
44             while(j+1 < n && a[j-1] * a[j+1] == a[j] * a[j]) j++;
45             ans = max(ans,j-i+1);
46             i = j;
47         //    printf("j = %d  ans = %d
",j,ans);
48         }
49         printf("%d
",max(res,ans));
50     }
51     return 0;
52 }
View Code
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 1000005;
 8 int dp1[maxn],dp2[maxn];
 9 int n;
10 long long  a[maxn];
11 
12 int ok1(int x){
13     if(2*a[x] == a[x-1] + a[x+1]) return 1;
14     return 0;
15 }
16 
17 int ok2(int x){
18     if(a[x] * a[x] == a[x-1] * a[x+1]) return 1;
19     return 0;
20 }
21 
22 int main(){
23     int T;
24     scanf("%d",&T);
25     while(T--){
26         scanf("%d",&n);
27         for(int i = 1;i <= n;i++) scanf("%I64d",&a[i]);
28         if(n <= 2){
29             printf("%d
",n);
30             continue;
31         }
32         for(int i = 1;i <= n;i++) dp1[i] = dp2[i] = 2;
33         
34         for(int i = 2;i < n;i++) {
35             if(ok1(i)) dp1[i] = dp1[i-1] + 1;
36             if(ok2(i)) dp2[i] = dp2[i-1] + 1;
37         }
38         int ans = 2;
39         for(int i = 1;i <= n;i++) ans = max(ans,max(dp1[i],dp2[i]));
40         printf("%d
",ans);
41     }
42     return 0;
43 }
View Code

1003 Question for the Leader

1004 Route Statistics

1005 Simple Problem

1006 Test for Rikka

1007 Undirected Graph

1008 Virtual Participation

 1009 Walk Out

先从起点开始尽量走0,然后找到一堆距离终点最近的1,看作一层的1,然后从这些1出发,一定是向右边或者是下面走的

因为第一个1的高位已经确定了,位数短的串比位数长的串小

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<vector>
 7 using namespace std;
 8 
 9 char g[1005][1005];
10 int n,m;//n行,m列
11 int vis[1005][1005];
12 int dir[4][2] = {1,0,-1,0,0,1,0,-1};
13 
14 int ok(int x,int y){
15     if(vis[x][y] || x < 1 || x > n || y < 1 || y > m) return 0;
16     return 1;
17 }
18 
19 void bfs(){
20     queue< pair<int, int> > q; 
21     memset(vis,0,sizeof(vis));vis[1][1] = 1;
22     int mx = 2;
23     q.push(make_pair(1,1));
24     while(!q.empty()){
25         int x = q.front().first;
26         int y = q.front().second;q.pop();
27 
28         if(g[x][y] == '0'){
29             for(int i = 0;i < 4;i++){
30                 int xx = x+dir[i][0];
31                 int yy = y+dir[i][1];
32                 if( !ok(xx,yy)) continue;
33                 q.push(make_pair(xx,yy)) ;
34                 mx = max(mx,xx+yy);vis[xx][yy] = 1;
35             }
36         }
37     }
38     
39     if(vis[n][m] && g[n][m] == '0') {
40         puts("0");
41         return ;
42     }
43     
44     printf("1");
45 
46     for(int i = mx;i < n+m ;i++){    
47         char mn = '1';
48         for(int x = 1;x <= n;x++){
49             int y = i - x;
50             if(y < 1 ||y > m || !vis[x][y]) continue;
51             mn = min(mn,g[x+1][y]);
52             mn = min(mn,g[x][y+1]);
53         }
54         printf("%c",mn);
55         
56         for(int x = 1;x <= n;x++){
57             int y = i-x;
58             if(y < 1 || y > m || !vis[x][y]) continue;
59             if(g[x+1][y] == mn) vis[x+1][y] = 1;
60             if(g[x][y+1] == mn) vis[x][y+1] = 1;
61         }
62     }
63     puts("");
64 }
65 
66 int main(){
67     int T;
68     scanf("%d",&T);
69     while(T--){
70         scanf("%d %d",&n,&m);
71         for(int i = 1;i <= n;i++) scanf("%s",g[i] + 1);
72         for(int i = 1;i <= n;i++) g[i][m+1] = '2';
73         for(int i = 1;i <= m;i++) g[n+1][i] = '2';
74         bfs();
75     }
76     return 0;
77 }
View Code

1010 XYZ and Drops

1011 Yet Another XYZ Problem

1012 ZZX and Permutations

原文地址:https://www.cnblogs.com/wuyuewoniu/p/4692203.html