码迷,mamicode.com
首页 > 其他好文 > 详细

Topcoder SRM 655 DIV1 250 CountryGroupHard

时间:2015-03-18 12:10:34      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

题意:不同国家的人站在一起,同一个国家的人相邻站,给你一个数组,表示 这个位置属于的国家有几个人,0可以是任何数字,问你能不能确定这个数组(只有一个解)

解题思路:搜索

解题代码:

技术分享
  1 // BEGIN CUT HERE
  2 /*
  3 
  4 */
  5 // END CUT HERE
  6 #line 7 "CountryGroupHard.cpp"
  7 #include <cstdlib>
  8 #include <cctype>
  9 #include <cstring>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <string>
 15 #include <iostream>
 16 #include <sstream>
 17 #include <map>
 18 #include <set>
 19 #include <queue>
 20 #include <stack>
 21 #include <fstream>
 22 #include <numeric>
 23 #include <iomanip>
 24 #include <bitset>
 25 #include <list>
 26 #include <stdexcept>
 27 #include <functional>
 28 #include <utility>
 29 #include <ctime>
 30 using namespace std;
 31 
 32 #define PB push_back
 33 #define MP make_pair
 34 
 35 #define REP(i,n) for(i=0;i<(n);++i)
 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i)
 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i)
 38 
 39 typedef vector<int> VI;
 40 typedef vector<string> VS;
 41 typedef vector<double> VD;
 42 typedef long long LL;
 43 typedef pair<int,int> PII;
 44 
 45 int color[1000];
 46 int tmp[1000];
 47 int n ;
 48 int ok = 0 ; 
 49 void isok()
 50 {
 51    int tok = 0 ; 
 52    for(int i = 0 ;i < n;i ++)
 53    {
 54       if(color[i] != 0 ) 
 55           tok = 1;
 56       if(i >= 1 && color[i] == 0 && color[i-1] == 0 )
 57           ok = 2; 
 58    }
 59    if(tok == 0 && n != 1)
 60        return;
 61    
 62    ok ++ ; 
 63 }
 64 void dosolve(int k,int be, int c)
 65 {
 66     if(ok == 2)
 67         return;
 68      if(k == n)
 69      {
 70         isok();
 71         return;
 72      }
 73      if(tmp[k] == 0)
 74      {
 75          dosolve(k+1,be,c);
 76          return;
 77      }
 78      if(be > k)
 79      {
 80        if(tmp[k] == c )
 81        {
 82          dosolve(k+1,be,c);
 83        }
 84        else return;
 85      }else{
 86        for(int j = max(be,k-tmp[k]+1); j <= k;j ++)
 87        {
 88           if(j + tmp[k] > n)
 89               break;
 90           for(int s = j ; s < j + tmp[k]; s ++)
 91               color[s] = 1; 
 92           dosolve(k+1,j + tmp[k],tmp[k]);
 93           for(int s = j ; s < j + tmp[k]; s ++)
 94               color[s] = 0; 
 95        }
 96      }
 97 }
 98 class CountryGroupHard
 99 {
100         public:
101         string solve(vector <int> a)
102         {
103             ok = 0 ;
104             n = a.size();
105             for(int i= 0 ;i< n;i ++)
106                 tmp[i] = a[i];
107             dosolve(0,0,-1);
108             //printf("%d\n",ok);            
109             if(ok == 1 )
110             return "Sufficient";
111             return "Insufficient";
112         }
113         
114 // BEGIN CUT HERE
115     public:
116     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
117     private:
118     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << \" << *iter << "\","; os << " }"; return os.str(); }
119     void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << \" << endl; cerr << "\tReceived: \"" << Received << \" << endl; } }
120     void test_case_0() { int Arr0[] = {0,2,3,0,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Sufficient"; verify_case(0, Arg1, solve(Arg0)); }
121     void test_case_1() { int Arr0[] = {0,2,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Insufficient"; verify_case(1, Arg1, solve(Arg0)); }
122     void test_case_2() { int Arr0[] = {0,3,0,0,3,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Sufficient"; verify_case(2, Arg1, solve(Arg0)); }
123     void test_case_3() { int Arr0[] = {0,0,3,3,0,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Insufficient"; verify_case(3, Arg1, solve(Arg0)); }
124     void test_case_4() { int Arr0[] = {2,2,0,2,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Sufficient"; verify_case(4, Arg1, solve(Arg0)); }
125 
126 // END CUT HERE
127 
128 };
129 
130 // BEGIN CUT HERE
131 int main()
132 {
133         CountryGroupHard ___test;
134         ___test.run_test(-1);
135         return 0;
136 }
137 // END CUT HERE
View Code

 

Topcoder SRM 655 DIV1 250 CountryGroupHard

标签:

原文地址:http://www.cnblogs.com/zyue/p/4346535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!