Topcoder open 2015 Round 1A 250 Similars 枚举 + 状压

题意:问你A-B之间 问你选两个数字 使得 这两个数字 相同 数字 个数最多 的 相同数字个数。

解题思路:枚举A-B之间所有的数,把数分解成10进制hash状态压缩,然后再把所有压缩后的值 N^2枚举找出最大值。

解题代码:

  1 // BEGIN CUT HERE
  2 /*
  3
  4 */
  5 // END CUT HERE
  6 #line 7 "Similars.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 map <LL,int > mp;
 46 map <LL,int >::iterator ti,tj;
 47 int hs[11];
 48
 49 int pp(LL a,LL b)
 50 {
 51    int ans = 0 ;
 52    while(a && b)
 53    {
 54        if(a&b&1 == 1 )
 55        {
 56           ans ++ ;
 57        }
 58        a = a >> 1;
 59        b = b >> 1;
 60    }
 61    return ans;
 62 }
 63 void change(int L)
 64 {
 65   memset(hs,0,sizeof(hs));
 66   int t = 0 ;
 67   while(L)
 68   {
 69     hs[L % 10] = 1;
 70     L/= 10 ;
 71   }
 72   LL tmp = 0 ;
 73   for(int i = 0 ;i <= 9;i ++)
 74   {
 75      if(hs[i])
 76          tmp += (1<<i);
 77   }
 78   //printf("%lld\n",tmp);
 79   mp[tmp] ++;
 80 }
 81 class Similars
 82 {
 83         public:
 84         int maxsim(int L, int R)
 85         {
 86             mp.clear();
 87             int ans = 0 ;
 88              for(int i = L ;i <= R ;i ++)
 89              {
 90                 change(i);
 91              }
 92              for(ti = mp.begin();ti != mp.end();ti++)
 93                  for(tj = ti ;tj != mp.end();tj++)
 94                  {
 95                      if(ti == tj)
 96                      {
 97                         if(ti->second >= 2)
 98                         {
 99                            ans = max(ans,pp(ti->first,tj->first));
100                         }
101                      }else{
102                            ans = max(ans,pp(ti->first,tj->first));
103                      }
104                  }
105              return ans ;
106         }
107
108 // BEGIN CUT HERE
109     public:
110     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(); }
111     private:
112     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(); }
113     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; } }
114     void test_case_0() { int Arg0 = 1; int Arg1 = 10; int Arg2 = 1; verify_case(0, Arg2, maxsim(Arg0, Arg1)); }
115     void test_case_1() { int Arg0 = 1; int Arg1 = 99; int Arg2 = 2; verify_case(1, Arg2, maxsim(Arg0, Arg1)); }
116     void test_case_2() { int Arg0 = 99; int Arg1 = 100; int Arg2 = 0; verify_case(2, Arg2, maxsim(Arg0, Arg1)); }
117     void test_case_3() { int Arg0 = 1000; int Arg1 = 1010; int Arg2 = 2; verify_case(3, Arg2, maxsim(Arg0, Arg1)); }
118     void test_case_4() { int Arg0 = 444; int Arg1 = 454; int Arg2 = 2; verify_case(4, Arg2, maxsim(Arg0, Arg1)); }
119
120 // END CUT HERE
121
122 };
123
124 // BEGIN CUT HERE
125 int main()
126 {
127         Similars ___test;
128         ___test.run_test(-1);
129         return 0;
130 }
131 // END CUT HERE

时间: 2024-08-28 16:36:23

Topcoder open 2015 Round 1A 250 Similars 枚举 + 状压的相关文章

Google Code Jam 2015 Round 1A Mushroom Monster 水

题意:每10秒扫描一下盘子,查看盘子里面的面包个数,问你主角用两种吃法可能吃得的最少的面包. 1)任意吃. 2)每秒以恒定速度. 解题思路:暴力,找差值. 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月18日 星期六 09时52分04秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #includ

Google Code Jam 2015 Round 1A Haircut 二分

题意:给你每个理发师的理发时间,问你排在队列中的第N个位置,问你应该被哪个理发师剪发. 解题思路:二分时间,看这个时间到第几个人理发了,然后找到临界值,看这个值的时候有那些理发师接待了新旅客的,即可找到那个理发师. 解题代码: 1 // File Name: b.sample.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月18日 星期六 09时05分30秒 4 5 #include<vector> 6 #include<lis

hdu5305(2015多校2)--Friends(状压,深搜)

Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 668    Accepted Submission(s): 313 Problem Description There are n people and m pairs of friends. For every pair of friends, they can cho

Codeforces Round #384 (Div. 2) //复习状压... 罚时爆炸 BOOM

不想欠题了..... 多打打CF才知道自己智商不足啊... A. Vladik and flights 给你一个01串  相同之间随便飞 没有费用 不同的飞需要费用为  abs i-j 真是题意杀啊,,,实际上我们只考虑01转换的代价一定为1如果目的地和起点相同  费用为0 不相同  肯定为1  因为0旁边必然有1 #include <cstdio> #include <iostream> #include <algorithm> #include <vector

Educational Codeforces Round 76 F 折半枚举

Educational Codeforces Round 76 F 折半枚举 https://codeforces.com/contest/1257/problem/F 题意: 数组a,找到一个x使得a中每一个元素异或x后"二进制中1的个数"相同. 数组长度100,数字大小2^30. 思路: 折半枚举答案X,如分为X前15位和后15位.之后我们再枚举期望的"相同个数"是多少,通过hash看看能不能满足就好了. 代码: #include <bits/stdc++

HDU Untitled(状压DP OR dfs枚举子集)

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 169 Problem Description There is an integer a and n integers b1,-,bn. After selecting some numbers from b

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

uva 11825 Hackers&amp;#39; Crackdown (状压dp,子集枚举)

题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪(没有计算机有该项服务). 思路:(见大白70页,我的方程与大白不同) 把n个集合P1.P2.Pn分成尽量多的组,使得每组中全部集合的并集等于全集,这里的集合Pi是计算机i及其相邻计算机的集合,用cover[i]表示若干Pi的集合S中全部集合的并集,dp[s]表示子集s最多能够分成多少组,则 假设co

uva 11825 Hackers&#39; Crackdown (状压dp,子集枚举)

题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有相同的n种服务),对每台计算机,你可以选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让更多的服务瘫痪(没有计算机有该项服务). 思路:(见大白70页,我的方程与大白不同) 把n个集合P1.P2.Pn分成尽量多的组,使得每组中所有集合的并集等于全集,这里的集合Pi是计算机i及其相邻计算机的集合,用cover[i]表示若干Pi的集合S中所有集合的并集,dp[s]表示子集s最多可以分成多少组,则 如果cove