Internet of Lights and Switches
题目描述
You are a fan of "Internet of Things"(IoT, 物联网), so you build a nice Internet of Lights and Switches in your huge mansion. Formally, there are n lights and m switches, each switch controls one or more lights, i.e. pressing that switch flips the status of those lights (on->off, off->on).
Initially, all the lights are on. Your task is to count the number of ways to turn off all the lights by pressing some consecutive switches. Each switch should not be pressed more than once. There is only one restriction: the number of switches you pressed should be between a and b (inclusive).
输入描述
There will be at most 20 test cases. Each test case begins with a line containing four integers n, m, a, b (2<=n<=50, 1<=a<=b<=m<=300000). Each of the following m lines contains a 01 string of length n. The i-th character is 1 if and only if that switch controls the i-th light. The size of the whole input file does not exceed 8MB.
输出描述
For each test case, print the case number, and the number of ways to turn off all the lights.
输入样例
2 4 1 4 01 10 11 00 2 4 3 3 01 10 11 00 6 3 1 3 101001 010110 101001
输出样例
Case 1: 3 Case 2: 0 Case 3: 2
这道题目运用二分, 有N盏灯和M个开关,每一个开关控制多盏灯(比如N=4,"0011"就代表这个开关控制第3和第4盏灯),现在问你有多少种按开关的方法使得所有的灯都熄灭(只能按一次开关,并且按的开关的编号是连续的),另外所按的编号连续的开关的长度在[a,b]范围内。初始所有的灯都是开着的。思路:二分+位运算+前缀和,这道题目可以以最后的边界为枚举点,也可以以最前面的边界为枚举点,这两份代码都会提供其中,如果大家觉得C++的语法速度慢的话,代码中有IO_Init()是用来消除C和C++耗时差异的,加上这个速度就快了。第二份代码的时间复杂度为O(nlogn),而第一份代码的时间复杂度因数据而定代码1:以最后边界为枚举点/*头文件模板*/ #include <map> #include <set> #include <cmath> #include <ctime> #include <queue> #include <vector> #include <cctype> #include <cstdio> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <iomanip> #include <typeinfo> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define pb push_back #define mp make_pair #define mem(a, x) memset(a, x, sizeof(a)) #define copy(a, b) memcpy(a, b, sizeof(a)) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define FIN freopen("input.txt", "r", stdin) #define FOUT freopen("output.txt", "w", stdout) typedef long long LL; typedef pair<int, int > PII; typedef pair<int, string> PIS; typedef unsigned long long uLL; template<typename T> void print (T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void print (const T &a, string bes = "") { int len = bes.length(); if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } void IO_Init() { ios::sync_with_stdio (false); } LL LLabs (LL a) { return a > 0 ? a : -a; } const double PI = 3.1415926535898; const double eps = 1e-10; const int MAXM = 1e5; const int MAXN = 3e5 +5; const LL INF = 0x3f3f3f3f; /*头文件模板*/ int n, m, a, b, tot; LL E_bin[MAXN]; char str[50 + 5]; struct point { LL v; int d; point(LL v, int d) : v(v), d(d) {} point() {} bool operator < (const point &a) const { return v < a.v; } } F[MAXN]; int main() { int cas = 1; IO_Init(); //freopen("D://imput.txt","r", stdin); while(cin >> n >> m >> a >> b) { tot = 0; LL u = 0, tar = 0,ans = 0;; for(int i = 0; i < n; i ++) { tar |= 1LL << i; } for(int i = 1; i <= m; i++) { cin >> str; u = 0; for(int j = 0; j < n; j ++) { if(str[j] == '1') u |= 1LL << j; } E_bin[i] = u; if(i != 1) { E_bin[i] ^= E_bin[i - 1]; } F[tot].v = E_bin[i]; F[tot ++].d = i; } sort(F, F + tot); for(int i = 1; i <= m; i ++) { int p = lower_bound(F, F + tot, point(E_bin[i] ^ tar, i)) - F; int q = upper_bound(F, F + tot, point(E_bin[i] ^ tar, i)) - F; if(E_bin[i] == tar && i >= a && i <= b) ans ++; if(F[p].v != (E_bin[i] ^ tar)) continue; while(p < q) { if(i - F[p].d >= a && i - F[p].d <= b) ans ++; p ++; } } printf("Case %d: %lld\n", cas ++, ans); } return 0; }
代码2:最开始的边界为枚举点
/*头文件模板*/ #include <map> #include <set> #include <cmath> #include <ctime> #include <queue> #include <vector> #include <cctype> #include <cstdio> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <iomanip> #include <typeinfo> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define pb push_back #define mp make_pair #define mem(a, x) memset(a, x, sizeof(a)) #define copy(a, b) memcpy(a, b, sizeof(a)) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define FIN freopen("input.txt", "r", stdin) #define FOUT freopen("output.txt", "w", stdout) typedef long long LL; typedef pair<int, int > PII; typedef pair<int, string> PIS; typedef unsigned long long uLL; template<typename T> void print (T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void print (const T &a, string bes = "") { int len = bes.length(); if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } void IO_Init() { ios::sync_with_stdio (false); } LL LLabs (LL a) { return a > 0 ? a : -a; } const double PI = 3.1415926535898; const double eps = 1e-10; const int MAXM = 1e5; const int MAXN = 3e5 +5; const LL INF = 0x3f3f3f3f; /*头文件模板*/ int n, m, a, b, tot; LL E_bin[MAXN]; char str[50 + 5]; struct point { LL v; int d; point(LL v, int d) : v(v), d(d) {} point() {} bool operator < (const point &a) const { if(v == a.v) return d < a.d; return v < a.v; } } F[MAXN]; int main() { int cas = 1; //FIN; IO_Init(); while(cin >> n >> m >> a >> b) { tot = 0; LL u = 0, tar = 0,ans = 0;; for(int i = 0; i < n; i ++) { tar |= 1LL << i; } E_bin[0] = 0; F[tot].v = 0; F[tot ++].d = 0; for(int i = 1; i <= m; i++) { cin >> str; u = 0; for(int j = 0; j < n; j ++) { if(str[j] == '1') u |= 1LL << j; } E_bin[i] = u; E_bin[i] ^= E_bin[i - 1]; F[tot].v = E_bin[i]; F[tot ++].d = i; } sort(F, F + tot); for(int i = 0; i <= m; i ++) { int p = lower_bound(F, F + tot, point(E_bin[i] ^ tar, i + a)) - F; int q = upper_bound(F, F + tot, point(E_bin[i] ^ tar, i + b)) - F; ans += q - p; } printf("Case %d: %lld\n", cas ++, ans); } return 0; }