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和 ...
随机推荐
- 阿里云服务器如何安装memcached
方法/步骤 1 使用Xshell登陆阿里云服务器. 请使用root帐号登陆.下面的操作全部在home目录里执行 2 安装libevent. 输入命令 yum -y install libevent-d ...
- tomcat+nginx+redis实现均衡负载、session共享(二)
今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html. 1.red ...
- angularjs modal 嵌套modal的问题
anguarjs中当遇到modal嵌套modal的时候,比如一个modal弹出啦一个modal1,关闭modal1后,modal本身的关闭功能失效,那么需要$modal来生命弹出的modal1并且关闭 ...
- sql查询过程中 update,insert,delete可视化收影响行数
insert into test_tb output inserted.id,inserted.data values('c'),('d') delete from test_tb output de ...
- (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- C++跨平台事件机制实现
今天看到有人在讨论C++标准没有提供类似操作系统层次的事件通知机制,如windows的事件内核对象.其实我想说的事,C++11标准里的互斥量及条件变量已经够帮我们实现类似的功能了. 刚编写了一个事件通 ...
- input file文件上传样式
<style> .file-group { position: relative; width: 200px; height: 80px; ...
- YII框架的部署 通过YII脚手架程序创建应用程序系统
1,把YII框架里面的framework复制粘贴到nginx目录下 2,创建一个商城系统: 1)修改环境变量 制定php.exe的目录 2)C:\Users\Administrator>cd C ...
- linux c静态链接库与动态链接库
库函数是我们编程的时候经常用到的,我们协作编程的时候可以将常用的函数封装成库供大家使用,这样能够提高大家的工作效率.对于库函数,它分为动态链接库和静态链接库.对于静态链接库我们必须是连接到可执行文件中 ...
- smali文件语法参考
Dalvik opcodes Author: Gabor Paller Vx values in the table denote a Dalvik register. Depending on th ...