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和 ...
随机推荐
- ratingBar抢焦点问题
ratingBar抢viewpager焦点问题: 1)写一个类继承ratingBar,让onTouchevent或者dispatchTouchEvent返回false 2)设置ratingBar的属性 ...
- Notepad++在编程使用时的小技巧
http://blog.csdn.net/freewaywalker/article/details/8010790 为了编程时更快捷和适应个人习惯,我们可以对Notepad++进行一系列的设置,这当 ...
- iOS UICollectionview的详细介绍
转载自:http://jinqianchina.github.io/2015/08/16/UICollectionview%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%A6%E8 ...
- JavaScript Array(数组) 对象
更多实例 合并两个数组 - concat() 合并三个数组 - concat() 用数组的元素组成字符串 - join() 删除数组的最后一个元素 - pop() 数组的末尾添加新的元素 - push ...
- java 从jar包中读取资源文件
在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...
- [转]Mysql导入导出工具Mysqldump和Source命令用法详解
Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...
- js登录页面的 回车事件
js登录页面的 回车事件 js登录页面的 回车事件(2012-12-26 10:37:03)转载▼标签: jseventkey回车事件登录 分类: js.jquery //回车事件 第一种docum ...
- ThinkPHP 自动验证与自动填充无效可能的原因
原文链接:http://www.5idev.com/p-thinkphp_validate_auto_Invalid.shtml 自动验证与自动填充是在使用ThinkPHP时经常用到的功能,但偶尔会遇 ...
- bom type:Phantom
bom的类型 'type': fields.selection([('normal','Normal BoM'),('phantom','Sets / Phantom')], 'BoM Type', ...
- 模块化编程AMD&CommonJS
为什么要模块化编程 如果JS也可以像类似python,Java使用import,引入我们想要的模块,想要什么模块,就加载什么模块,可以给前端编程带来更多的便捷,结构更加清晰明了.但是,这样做有一个前提 ...