`
bbsunchen
  • 浏览: 225146 次
  • 性别: Icon_minigender_1
  • 来自: 天朝帝都
社区版块
存档分类
最新评论

USACO Combination Lock (combo)题解

 
阅读更多

有人说这一题暴力搜索,O(N^3),naive,哥只要O(N^2)

当然也差不了多少啦哈哈,因为N只有5。。。

思路:

总数,减去overlap的,就是结果

 代码:

/*
ID: bbsunch2
PROG: combo
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>


using namespace std;

int main()
{
    ofstream fout ("combo.out");
    ifstream fin ("combo.in");
    int N;
    fin >> N;
    if (N == 1){
        fout << 1 << endl;
        return 0;
    }else if(N == 2){
        fout << 8 << endl;
        return 0;
    }else if(N == 3){
        fout << 27 << endl;
        return 0;
    }else if(N == 4){
        fout << 64 << endl;
        return 0;
    }else if(N == 5){
        fout << 125 << endl;
        return 0;
    }
    int total = 250;
    int num_list[6];
    fin >> num_list[0] >> num_list[1] >> num_list[2] >> num_list[3] >> num_list[4] >> num_list[5];
    vector<int> num_candidate[6];
    for(int i = 0; i < 6; i++){
        //cout << i << " =========" << endl;
        for(int j = 0; j < 5; j++){
            int a = num_list[i] - 2 + j;
            if (a <= 0){
                a = N + a;
            }else if (a == N){

            }else{
                a = a % N;
            }
            //cout << a << endl;
            bool exist = false;
            for(int k =0; k < num_candidate[i].size(); k++){
                if (a == num_candidate[i][k]){
                    exist = true;
                    break;
                }
            }
            if (!exist){
                num_candidate[i].push_back(a);
            }
        }
    }
    int overlap[3] = {0};
    for(int i = 0; i < 3; i++){
        int k = i + 3;
        //cout << num_candidate[i].size() << num_candidate[k].size() << endl;
        for (int m = 0; m < num_candidate[i].size(); m++){
            for(int n = 0; n < num_candidate[k].size(); n++){
                if(num_candidate[i][m] == num_candidate[k][n]){
                    overlap[i]++;
                    break;
                }
            }
        }
    }
    int subtract = overlap[0] * overlap[1] * overlap[2];
    fout << total - subtract << endl;

    return 0;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics