SRM 600(1-250pt,500pt)

DIV1 250pt

题意:给定一个vector<int>A,若能从里面选出一些数,使得他们按位或的值为x,则称x为吉利数。给定k,问最少要从A里面去掉多少个数,才能使k变为不吉利数。

解法:按位考虑。如果A中某元素A[i],将A[i]和k转化成二进制形式,如果某一位A[i]为1而k的为0,则一定不选选取掉A[i],把所有这样的A[i]全部从A中删掉。然后,维护ans,枚举所有k二进制为1的位,计A中有t个元素该位为1,则ans = min(ans, t)。

A.size() <= 50,A[i] <= 10^9

tag:按位或

Ps:我的代码写的太复杂了。。。

  1 // BEGIN CUT HERE
  2 /*
  3  * Author:  plum rain
  4  * score :
  5  */
  6 /*
  7 
  8  */
  9 // END CUT HERE
 10 #line 11 "ORSolitaire.cpp"
 11 #include <sstream>
 12 #include <stdexcept>
 13 #include <functional>
 14 #include <iomanip>
 15 #include <numeric>
 16 #include <fstream>
 17 #include <cctype>
 18 #include <iostream>
 19 #include <cstdio>
 20 #include <vector>
 21 #include <cstring>
 22 #include <cmath>
 23 #include <algorithm>
 24 #include <cstdlib>
 25 #include <set>
 26 #include <queue>
 27 #include <bitset>
 28 #include <list>
 29 #include <string>
 30 #include <utility>
 31 #include <map>
 32 #include <ctime>
 33 #include <stack>
 34 
 35 using namespace std;
 36 
 37 #define CLR(x) memset(x, 0, sizeof(x))
 38 #define CLR1(x) memset(x, -1, sizeof(x))
 39 #define PB push_back
 40 #define SZ(v) ((int)(v).size())
 41 #define zero(x) (((x)>0?(x):-(x))<eps)
 42 #define out(x) cout<<#x<<":"<<(x)<<endl
 43 #define tst(a) cout<<#a<<endl
 44 #define CINBEQUICKER std::ios::sync_with_stdio(false)
 45 
 46 typedef vector<int> VI;
 47 typedef vector<string> VS;
 48 typedef vector<double> VD;
 49 typedef pair<int, int> pii;
 50 typedef long long int64;
 51 
 52 const double eps = 1e-8;
 53 const double PI = atan(1.0)*4;
 54 const int maxint = 2139062143;
 55 
 56 int num[50];
 57 bool v[55];
 58 
 59 bool ok(int x, int y)
 60 {
 61     while (y){
 62         int t1 = x & 1, t2 = y & 1;
 63         if (!t2 && t1) return 0;
 64         x >>= 1; y >>= 1;
 65     }
 66     return !x;
 67 }
 68 
 69 void gao(int x)
 70 {
 71     int p = 0;
 72     while (x){
 73         num[p++] += (x & 1);
 74         x >>= 1;
 75     }
 76 }
 77 
 78 class ORSolitaire
 79 {
 80     public:
 81         int getMinimum(vector <int> nb, int x){
 82             int sz = nb.size();
 83             CLR (num); CLR (v);
 84             for (int i = 0; i < sz; ++ i){
 85                 v[i] = ok(nb[i], x);
 86                 if (v[i]) gao(nb[i]);
 87             }
 88 
 89             int p = 0;
 90             int ans = 100;
 91             while (x){
 92                 if (x & 1)
 93                     ans = min(ans, num[p]);
 94                 x >>= 1;
 95                 ++ p;
 96             }
 97             return ans;
 98         }
 99         
100 // BEGIN CUT HERE
101     public:
102     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(); }
103     private:
104     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(); }
105     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
106     void test_case_0() { int Arr0[] = {1, 2, 4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; int Arg2 = 1; verify_case(0, Arg2, getMinimum(Arg0, Arg1)); }
107     void test_case_1() { int Arr0[] = {1, 2, 4, 7, 8}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; int Arg2 = 2; verify_case(1, Arg2, getMinimum(Arg0, Arg1)); }
108     void test_case_2() { int Arr0[] = {12571295, 2174218, 2015120}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 0; verify_case(2, Arg2, getMinimum(Arg0, Arg1)); }
109     void test_case_3() { int Arr0[] = {5,2,4,52,62,9,8,3,1,11,6}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 11; int Arg2 = 3; verify_case(3, Arg2, getMinimum(Arg0, Arg1)); }
110     void test_case_4() { int Arr0[] = {503, 505, 152, 435, 491, 512, 1023, 355, 510, 500, 502, 255, 63, 508, 509, 511, 60, 250, 254, 346}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 510; int Arg2 = 5; verify_case(4, Arg2, getMinimum(Arg0, Arg1)); }
111 
112 // END CUT HERE
113 
114 };
115 
116 // BEGIN CUT HERE
117 int main()
118 {
119 //    freopen( "a.out" , "w" , stdout );    
120     ORSolitaire ___test;
121     ___test.run_test(-1);
122        return 0;
123 }
124 // END CUT HERE
View Code

 

DIV1 600pt

题意:有一个矩阵每位只可能为1或0,每次操作可以将某一为的0变1或者1变0,求最少操作次数,使得操作以后,该矩阵有rnum行为回文,有cnum列为回文。

矩阵行数 <= 14,列数 <= 14,行数列数均为偶数。

解法:枚举行,dp列。用O(2^14)的复杂度枚举哪些行为对称行。处理列的时候,把关于中间对称的两列一起处理,每对列都可以看成4种物品,两列都不对称,左列对称右列不对称,右列对称左列不对称,都对称,然后用背包处理就行了。

tag:dp, 背包, good

  1 // BEGIN CUT HERE
  2 /*
  3 
  4 */
  5 // END CUT HERE
  6 #line 7 "PalindromeMatrix.cpp"
  7 #include <cstdlib>
  8 #include <cctype>
  9 #include <cstring>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <iostream>
 15 #include <sstream>
 16 #include <set>
 17 #include <queue>
 18 #include <fstream>
 19 #include <numeric>
 20 #include <iomanip>
 21 #include <bitset>
 22 #include <list>
 23 #include <stdexcept>
 24 #include <functional>
 25 #include <string>
 26 #include <utility>
 27 #include <map>
 28 #include <ctime>
 29 #include <stack>
 30 
 31 using namespace std;
 32 
 33 #define clr0(x) memset(x, 0, sizeof(x))
 34 #define clr1(x) memset(x, -1, sizeof(x))
 35 #define PB push_back
 36 #define sz(v) ((int)(v).size())
 37 #define out(x) cout<<#x<<":"<<(x)<<endl
 38 #define tst(a) cout<<#a<<endl
 39 #define CINBEQUICKER std::ios::sync_with_stdio(false)
 40 
 41 typedef vector<int> VI;
 42 typedef vector<string> VS;
 43 typedef vector<double> VD;
 44 typedef long long int64;
 45 
 46 const double eps = 1e-8;
 47 const double PI = atan(1.0)*4;
 48 const int maxint = 2139062143;
 49 
 50 inline int MyMod( int a , int b ) { return (a%b+b)%b;}
 51 
 52 VS a;
 53 bool v[20];
 54 int d[20][50];
 55 
 56 int gao(int sta, int n, int m, int cnum)
 57 {
 58     int nmid = (n - 1) >> 1, mmid = (m - 1) >> 1;
 59     clr0 (v);
 60     for (int i = 0; i < n; ++ i)
 61         if (sta & (1 << i)) v[i] = 1;
 62 
 63     int c[20][4];
 64     for (int i = 0; i <= mmid; ++ i){
 65         clr0 (c[i]);
 66         for (int j = 0; j <= nmid; ++ j){
 67             int ii = m - 1 - i, jj = n - 1 - j;
 68             int t0 = 0;
 69             if (a[jj][i] == '0') ++ t0;
 70             if (a[jj][ii] == '0') ++  t0;
 71             if (a[j][i] == '0') ++ t0;
 72             if (a[j][ii] == '0') ++ t0;
 73             if (v[j] && v[jj]){
 74                 c[i][0] += (a[j][i] != a[j][ii]) + (a[jj][i] != a[jj][ii]);
 75                 int tmp = min(t0, 4 - t0);
 76                 for (int j = 1; j < 4; ++ j) c[i][j] += tmp;
 77             }
 78             if (v[j] && !v[jj]){
 79                 c[i][0] += (a[j][i] != a[j][ii]);
 80                 if (a[j][i] == a[j][ii]){
 81                     c[i][1] += (a[j][i] != a[jj][i]);
 82                     c[i][2] += (a[j][i] != a[jj][ii]);
 83                 }
 84                 else{
 85                     ++ c[i][1]; ++ c[i][2];
 86                 }
 87                 c[i][3] += min(t0, 4-t0);
 88             }
 89             if (!v[j] && v[jj]){
 90                 c[i][0] += (a[jj][i] != a[jj][ii]);
 91                 if (a[jj][i] == a[jj][ii]){
 92                     c[i][1] += (a[j][i] != a[jj][ii]);
 93                     c[i][2] += (a[j][ii] != a[jj][ii]);
 94                 }
 95                 else{
 96                     ++ c[i][1]; ++ c[i][2];
 97                 }
 98                 c[i][3] += min(t0, 4-t0);
 99             }
100             if (!v[j] && !v[jj]){
101                 int t1 = (a[j][i] != a[jj][i]), t2 = (a[j][ii] != a[jj][ii]);
102                 c[i][1] += t1; c[i][2] += t2; c[i][3] += t1 + t2;
103             }
104         }
105     }
106 
107     clr1 (d);
108     d[0][0] = c[0][0]; d[0][1] = min(c[0][1], c[0][2]); d[0][2] = c[0][3];
109     for (int i = 1; i <= mmid; ++ i)
110         for (int j = 0; j <= m; ++ j){
111             if (d[i-1][j] >= 0) d[i][j] = d[i-1][j] + c[i][0];
112             if (j && d[i-1][j-1] >= 0) d[i][j] = min(d[i][j]>=0 ? d[i][j] : n*m+10, d[i-1][j-1] + min(c[i][1], c[i][2]));
113             if (j >= 2 && d[i-1][j-2] >= 0) d[i][j] = min(d[i][j]>=0 ? d[i][j] : n*m+10, d[i-1][j-2] + c[i][3]);
114         }
115 
116     int ret = n * m + 10;
117     for (int i = cnum; i <= m; ++ i){
118         if (d[mmid][i] == -1) continue;
119         ret = min (ret, d[mmid][i]);
120     }
121     return ret;
122 }
123 
124 class PalindromeMatrix
125 {
126     public:
127         int minChange(vector <string> A, int rnum, int cnum){
128             a = A;
129             int n = sz(a), m = sz(a[0]);
130             int ans = n * m + 10, cnt = 1 << n;
131             for (int i = 0; i < cnt; ++ i)
132                 if (__builtin_popcount(i) >= rnum) ans = min(ans, gao(i, n, m, cnum));
133             return ans;
134         }
135         
136 // BEGIN CUT HERE
137     public:
138     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(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); }
139     //void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0();}
140     private:
141     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(); }
142     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
143     void test_case_0() { string Arr0[] = {"0000"
144 ,"1000"
145 ,"1100"
146 ,"1110"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 2; int Arg3 = 1; verify_case(0, Arg3, minChange(Arg0, Arg1, Arg2)); }
147     void test_case_1() { string Arr0[] = {"0000"
148 ,"1000"
149 ,"1100"
150 ,"1110"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 2; int Arg3 = 3; verify_case(1, Arg3, minChange(Arg0, Arg1, Arg2)); }
151     void test_case_2() { string Arr0[] = {"01"
152 ,"10"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 1; int Arg3 = 1; verify_case(2, Arg3, minChange(Arg0, Arg1, Arg2)); }
153     void test_case_3() { string Arr0[] = {"1110"
154 ,"0001"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 0; int Arg3 = 0; verify_case(3, Arg3, minChange(Arg0, Arg1, Arg2)); }
155     void test_case_4() { string Arr0[] = {"01010101"
156 ,"01010101"
157 ,"01010101"
158 ,"01010101"
159 ,"01010101"
160 ,"01010101"
161 ,"01010101"
162 ,"01010101"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 3; int Arg3 = 8; verify_case(4, Arg3, minChange(Arg0, Arg1, Arg2)); }
163     void test_case_5() { string Arr0[] = {"000000000000"
164 ,"011101110111"
165 ,"010001010101"
166 ,"010001010101"
167 ,"011101010101"
168 ,"010101010101"
169 ,"010101010101"
170 ,"011101110111"
171 ,"000000000000"
172 ,"000000000000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 5; int Arg2 = 9; int Arg3 = 14; verify_case(5, Arg3, minChange(Arg0, Arg1, Arg2)); }
173     void test_case_6() { string Arr0[] = {"11111101001110"
174 ,"11000111111111"
175 ,"00010101111001"
176 ,"10110000111111"
177 ,"10000011010010"
178 ,"10001101101101"
179 ,"00101010000001"
180 ,"10111010100100"
181 ,"11010011110111"
182 ,"11100010110110"
183 ,"00100101010100"
184 ,"01001011001000"
185 ,"01010001111010"
186 ,"10100000010011"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; int Arg2 = 8; int Arg3 = 31; verify_case(6, Arg3, minChange(Arg0, Arg1, Arg2)); }
187 
188 // END CUT HERE
189 
190 };
191 //by plum rain
192 // BEGIN CUT HERE
193 int main()
194 {
195     //freopen( "a.out" , "w" , stdout );    
196     PalindromeMatrix ___test;
197     ___test.run_test(-1);
198        return 0;
199 }
200 // END CUT HERE
View Code

 

 

 

posted @ 2013-12-23 17:12  Plumrain  阅读(207)  评论(0编辑  收藏  举报