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:我的代码写的太复杂了。。。
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "ORSolitaire.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; int num[];
bool v[]; bool ok(int x, int y)
{
while (y){
int t1 = x & , t2 = y & ;
if (!t2 && t1) return ;
x >>= ; y >>= ;
}
return !x;
} void gao(int x)
{
int p = ;
while (x){
num[p++] += (x & );
x >>= ;
}
} class ORSolitaire
{
public:
int getMinimum(vector <int> nb, int x){
int sz = nb.size();
CLR (num); CLR (v);
for (int i = ; i < sz; ++ i){
v[i] = ok(nb[i], x);
if (v[i]) gao(nb[i]);
} int p = ;
int ans = ;
while (x){
if (x & )
ans = min(ans, num[p]);
x >>= ;
++ p;
}
return ans;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
private:
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(); }
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; } }
void test_case_0() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getMinimum(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {, , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getMinimum(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getMinimum(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {,,,,,,,,,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getMinimum(Arg0, Arg1)); }
void test_case_4() { int Arr0[] = {, , , , , , , , , , , , , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, getMinimum(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
ORSolitaire ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
DIV1 600pt
题意:有一个矩阵每位只可能为1或0,每次操作可以将某一为的0变1或者1变0,求最少操作次数,使得操作以后,该矩阵有rnum行为回文,有cnum列为回文。
矩阵行数 <= 14,列数 <= 14,行数列数均为偶数。
解法:枚举行,dp列。用O(2^14)的复杂度枚举哪些行为对称行。处理列的时候,把关于中间对称的两列一起处理,每对列都可以看成4种物品,两列都不对称,左列对称右列不对称,右列对称左列不对称,都对称,然后用背包处理就行了。
tag:dp, 背包, good
// BEGIN CUT HERE
/* */
// END CUT HERE
#line 7 "PalindromeMatrix.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define sz(v) ((int)(v).size())
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; inline int MyMod( int a , int b ) { return (a%b+b)%b;} VS a;
bool v[];
int d[][]; int gao(int sta, int n, int m, int cnum)
{
int nmid = (n - ) >> , mmid = (m - ) >> ;
clr0 (v);
for (int i = ; i < n; ++ i)
if (sta & ( << i)) v[i] = ; int c[][];
for (int i = ; i <= mmid; ++ i){
clr0 (c[i]);
for (int j = ; j <= nmid; ++ j){
int ii = m - - i, jj = n - - j;
int t0 = ;
if (a[jj][i] == '') ++ t0;
if (a[jj][ii] == '') ++ t0;
if (a[j][i] == '') ++ t0;
if (a[j][ii] == '') ++ t0;
if (v[j] && v[jj]){
c[i][] += (a[j][i] != a[j][ii]) + (a[jj][i] != a[jj][ii]);
int tmp = min(t0, - t0);
for (int j = ; j < ; ++ j) c[i][j] += tmp;
}
if (v[j] && !v[jj]){
c[i][] += (a[j][i] != a[j][ii]);
if (a[j][i] == a[j][ii]){
c[i][] += (a[j][i] != a[jj][i]);
c[i][] += (a[j][i] != a[jj][ii]);
}
else{
++ c[i][]; ++ c[i][];
}
c[i][] += min(t0, -t0);
}
if (!v[j] && v[jj]){
c[i][] += (a[jj][i] != a[jj][ii]);
if (a[jj][i] == a[jj][ii]){
c[i][] += (a[j][i] != a[jj][ii]);
c[i][] += (a[j][ii] != a[jj][ii]);
}
else{
++ c[i][]; ++ c[i][];
}
c[i][] += min(t0, -t0);
}
if (!v[j] && !v[jj]){
int t1 = (a[j][i] != a[jj][i]), t2 = (a[j][ii] != a[jj][ii]);
c[i][] += t1; c[i][] += t2; c[i][] += t1 + t2;
}
}
} clr1 (d);
d[][] = c[][]; d[][] = min(c[][], c[][]); d[][] = c[][];
for (int i = ; i <= mmid; ++ i)
for (int j = ; j <= m; ++ j){
if (d[i-][j] >= ) d[i][j] = d[i-][j] + c[i][];
if (j && d[i-][j-] >= ) d[i][j] = min(d[i][j]>= ? d[i][j] : n*m+, d[i-][j-] + min(c[i][], c[i][]));
if (j >= && d[i-][j-] >= ) d[i][j] = min(d[i][j]>= ? d[i][j] : n*m+, d[i-][j-] + c[i][]);
} int ret = n * m + ;
for (int i = cnum; i <= m; ++ i){
if (d[mmid][i] == -) continue;
ret = min (ret, d[mmid][i]);
}
return ret;
} class PalindromeMatrix
{
public:
int minChange(vector <string> A, int rnum, int cnum){
a = A;
int n = sz(a), m = sz(a[]);
int ans = n * m + , cnt = << n;
for (int i = ; i < cnt; ++ i)
if (__builtin_popcount(i) >= rnum) ans = min(ans, gao(i, n, m, cnum));
return ans;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); if ((Case == -) || (Case == )) test_case_6(); }
//void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0();}
private:
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(); }
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; } }
void test_case_0() { string Arr0[] = {""
,""
,""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; verify_case(, Arg3, minChange(Arg0, Arg1, Arg2)); }
void test_case_1() { string Arr0[] = {""
,""
,""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; verify_case(, Arg3, minChange(Arg0, Arg1, Arg2)); }
void test_case_2() { string Arr0[] = {""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; verify_case(, Arg3, minChange(Arg0, Arg1, Arg2)); }
void test_case_3() { string Arr0[] = {""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; verify_case(, Arg3, minChange(Arg0, Arg1, Arg2)); }
void test_case_4() { string Arr0[] = {""
,""
,""
,""
,""
,""
,""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; verify_case(, Arg3, minChange(Arg0, Arg1, Arg2)); }
void test_case_5() { string Arr0[] = {""
,""
,""
,""
,""
,""
,""
,""
,""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; verify_case(, Arg3, minChange(Arg0, Arg1, Arg2)); }
void test_case_6() { string Arr0[] = {""
,""
,""
,""
,""
,""
,""
,""
,""
,""
,""
,""
,""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; verify_case(, Arg3, minChange(Arg0, Arg1, Arg2)); } // END CUT HERE };
//by plum rain
// BEGIN CUT HERE
int main()
{
//freopen( "a.out" , "w" , stdout );
PalindromeMatrix ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
SRM 600(1-250pt,500pt)的更多相关文章
- SRM 600 DIV1
A 按位讨论,取最小值; B 数据范围不大,首先要确定枚举角度; 状压枚举palindromes的列比较科学; 列确定后,目标就是求获得rcnt行的最小代价: dp[i][cnt]表示扫描到第i行,已 ...
- topcoder srm 600 div1
problem1 link 首先,如果一个数字的某一位是1但是$goal$的这一位不是1,那么这个数字是不用管它的.那么对于剩下的数字,只需要统计在$goal$为1的位上,这些数字对应位上也是1的数字 ...
- SRM 600 div 2 T 2
题意:给你50个数,问你最少去掉多少数能使得剩下的数不可能具备子集S,OR起来为goal 如果一个数不是goal的子状态,那么我们没必要删除他,所以我们只关心goal的子状态的数 1:如果所有的数OR ...
- SRM 600 div 2 T 1
贪心+枚举 #include <bits/stdc++.h> using namespace std; class TheShuttles { public: int getLeast ...
- Topcoder SRM 600 div1题解
日常TC计划正式启动! Easy(250pts): 题目大意:给你一个集合,里面一堆数,初始数为0,给你一个目标数,你可以选择集合中若干个数进行OR操作来得到目标数.问至少删去多少个数,使得你永远无法 ...
- SRM144 - SRM 148(少144-DIV1-LV3,147-DIV2-LV3)
SRM 144 DIV 1 500pt tag:组合 题意:彩票中奖.给定n, m,从1-n中选择m个数组成数列a1, a2, a3...am.对于数列{am}分别满足以下条件的概率: (1)数列所有 ...
- SRM475 - SRM479(1-250pt,500pt)
SRM 475 DIV1 300pt 题意:玩游戏.给一个棋盘,它有1×n(1行n列,每列标号分别为0,1,2..n-1)的格子,每个格子里面可以放一个棋子,并且给定一个只含三个字母WBR,长度为n的 ...
- SRM468 - SRM469(1-250pt, 500pt)
SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...
- SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)
SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...
随机推荐
- VS2010调试时候未响应
这几天使用vs2010,调试时候经常未响应,等了半天才缓过来,严重影响心情,决定解决这个问题. 搜寻一番,试着关闭VS,重新设置了vs2010的环境(在vs2010命令提示符下,执行devenv.ex ...
- 某PHP代码加密
<?php /* 本程序已加密: 2014-11-15 10:10:11 */ xs_run('JGxosS9QplmqLA6qjYo/LiX5ecUe0DH7p42Ww/Mdkf5/ybZDs ...
- 那些常用的eclipse快捷键
用了很久的eclipse了,有些快捷键常用,有的偶尔使用,现在记下常用的快捷键,以便大家和自己查用(持续更新) 这些快捷键都可以在[window]-[preferences]-[general]-[k ...
- javascript——迭代方法
<script type="text/javascript"> //五个迭代方法 都接受两个参数:要在每一项上运行的函数 和 运行该函数的作用域(可选) //every ...
- Perl数组: shift, unshift, push, pop
pop pop函数会删除并返回数组的最后一个元素. .. ; $fred = pop(@array); # $fred变成9,@array 现在是(5,6,7,8) $barney = pop @ar ...
- jQuery慢慢啃之属性(三)
1.attr(name|properties|key,value|fn)设置或返回被选元素的属性值. $("img").attr("src");//获取属性 $ ...
- grep操作
这个程序的名称来自Unix文本编辑器ed类似操作的命令: g/re/p 这个命令搜索整个文件中匹配给定正则表达式的文本行,并显示出来.有很多不同的命令行用于改变grep的默认行为,包括显示出不匹配的文 ...
- 系统默认字体Mac OS,Windows,XP,Lunix
可查获的信息太少,目前得知的是以中文为例, OS : Helvetical,Lucida Grande(西文默认字体) Windows 7: Microsoft Yahei Xp : Simsun,T ...
- html 中 #include file 的用法
有两个文件a.htm和b.htm,在同一目录下a.htm内容如下 <!-- #include file="b.htm" --> b.htm内容如下 今天:雨 31 ℃- ...
- 通用方法解决dedecms导航调用二级、三级栏目菜单
博客之前做网站的时候经常会遇到二级菜单.三级菜单.了解dede的人都知道从5.5版本开始都有二级菜单的调用方法了,网上也有不少的教程文章.不过这个调用需要修改dede源码的二级菜单样式.个人感觉不是很 ...