zoukankan      html  css  js  c++  java
  • Section 1.4 搜索

    Arithmetic Progressions

    http://www.wzoi.org/usaco/14%5C110.asp

    并没有看出哪里像搜索。。大概是我蠢,暴力过去了

    处理出所有的p^2+p^2的值,标记处有的值vis[i]

    在其中枚举a,以1-a+(n-1)*b<=2*m*m的限制枚举b,检查是否有n位的存在

    #include <bits/stdc++.h>
    using namespace std;
    const int N  = 140000;
    bool vis[N];
    int f[N>>1];
    struct point{
        int a, b;
        point(){};
        point(int _a, int _b){a = _a, b = _b;};
    
        bool operator<(const point &I)const{
            if(b == I.b)    return a <I.a;
            return b <I.b;
        }
    }p[10004];
    int main()
    {
        freopen("ariprog.in","r",stdin);
        #ifndef poi
        freopen("ariprog.out","w",stdout);
        #endif
        int n, m, i, j, k, a, b;
        cin >> n >> m;
        int id = 0, no = 0;
        for(i = 0; i <= m; i++){
            for(j = i; j <= m; j++) {
                f[++id] = i * i + j * j;
                vis[f[id]] = true;
            }
        }
        sort(f + 1, f + id + 1);
        id = unique(f + 1, f + id + 1) - f;
        int limit = 2 * m * m;
        for(i = 1; i < id; i++){
            a = f[i];
         //   printf("!!%d
    ", a);
            for(j = 1; j*(n-1)+a <= limit; j++){
                for(k = 1; k < n; k++){
                    if(!vis[j*k+a]) break;
                }
                if(k == n)  p[++no] = point(a, j);
            }
        }
        if(no == 0){
            printf("NONE
    ");
            return 0;
        }
        sort(p + 1, p + no + 1);
    
        for(i = 1; i <= no; i++){
            printf("%d %d
    ", p[i].a, p[i].b);
        }
    
        return 0;
    }
    View Code

    Mother's Milk

    http://www.wzoi.org/usaco/12%5C403.asp

    真。搜索。。以(0, 0, C)为初始,bfs一下,当a为0时记录c的值,因为很小,避免去重用vis标记

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 22;
    struct point{
        int a, b, c;
    
        point(){};
        point(int _a, int _b, int _c){a = _a, b = _b, c = _c;};
    };
    bool state[N][N][N], ok[N];
    int A, B, C;
    vector<int>ans;
    void bfs(){
        state[0][0][C] = true;
        queue<struct point>Q;
        Q.push(point(0, 0, C));
        struct point x;
        int temp, a, b, c;
        while(!Q.empty()){
            x = Q.front();
            if(x.a == 0)   ok[x.c] = true;
          // cout << x.a << x.b << x.c << endl;
            Q.pop();
            if(x.a){
                if(x.b != B){
                    temp = min(B-x.b, x.a);
                    a = x.a - temp;
                    b = x.b + temp;
                    c = x.c;
                   if(!state[a][b][c]){
                        Q.push(point(a, b, c));
                        state[a][b][c] = true;
                    }
                }
                if(x.c != C){
                    temp = min(C-x.c, x.a);
                    a = x.a - temp;
                    b = x.b;
                    c = x.c + temp;
                    if(!state[a][b][c]){
                        Q.push(point(a, b, c));
                        state[a][b][c] = true;
                    }
                }
            }
            if(x.b){
                if(x.a != A){
                    temp = min(A-x.a, x.b);
                    a = x.a + temp;
                    b = x.b - temp;
                    c = x.c;
                   if(!state[a][b][c]){
                        Q.push(point(a, b, c));
                        state[a][b][c] = true;
                    }
                }
                if(x.c != C){
                    temp = min(C-x.c, x.b);
                    a = x.a;
                    b = x.b - temp;
                    c = x.c + temp;
                   if(!state[a][b][c]){
                        Q.push(point(a, b, c));
                        state[a][b][c] = true;
                    }
                }
            }
            if(x.c){
                if(x.a != A){
                    temp = min(A-x.a, x.c);
                    a = x.a + temp;
                    b = x.b;
                    c = x.c - temp;
                   if(!state[a][b][c]){
                        Q.push(point(a, b, c));
                        state[a][b][c] = true;
                    }
                }
                if(x.b != B){
                    temp = min(B-x.b, x.c);
                    a = x.a;
                    b = x.b + temp;
                    c = x.c - temp;
                   if(!state[a][b][c]){
                        Q.push(point(a, b, c));
                        state[a][b][c] = true;
                    }
                }
            }
        }
    
    }
    int main()
    {
        freopen("milk3.in","r",stdin);
        #ifndef poi
        freopen("milk3.out","w",stdout);
        #endif
        cin >> A>> B>> C;
    
        bfs();
        bool first = true;
        for(int i = 0; i <= 20; i++){
    
            if(ok[i])  {
                if(!first)  cout << " ";
                cout << i;first = false;
            }
    
        }
        cout <<endl;
    
        return 0;
    }
    View Code

    考研好痛苦。好羡慕保研的。。

  • 相关阅读:
    Java Arrays.asList注意事项
    从给定的N个正数中选取若干个数之和最接近M
    java 8 foreach获取索引
    更优雅地关闭资源
    idea 下获取路径下文件的方法
    IDEA类和方法注释模板设置(非常详细)
    503 Error: need EHLO and AUTH first
    fatal error C1083: Cannot open include file: 'openssl/opensslv.h'
    'pip' 不是内部或外部命令
    JS中every()和some()的用法
  • 原文地址:https://www.cnblogs.com/bbbbbq/p/4641757.html
Copyright © 2011-2022 走看看