Parity Game
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 7567 |
|
Accepted: 2951 |
Description
Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask Hi M, whether this subsequence contains even or odd number of ones. Your friend answers Your question and you can ask the him about another subsequence and so on. Your task is to guess the entire sequence of numbers.
You suspect some of your friend's answers may isn't being correct and you want to convict him of falsehood. Thus you has decided to write a program to help you on this matter. The program would receive a series of your questions together with the answers that you had received from your friend. The aim of this program was to find the first answer which was provably wrong, i.e. that there exists a sequence satisfying Answers to all the previous questions, but no such sequence satisfies this answer.
Input
The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length was less or equal to 1000000000. The second line, there are one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer-question:two integers (the position of the first and last digit in The chosen subsequence) and one word which is either ' even ' or ' odd ' (the answer, i.e. the parity of the number of ones I n the chosen subsequence, where ' even ' means an even number of ones and ' odd ' means an odd number).
Output
There is only one line in output containing one integer x. Number x says that there exists a sequence of zeroes and ones s Atisfying first X parity conditions, but there exists none satisfying x+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should is the number of a ll the questions asked.
Sample Input
1051 2 Even3 4 odd5 6 even1 6 even7 Odd
Sample Output
3
Title: There is a known length of 01 strings, given a number of conditions, [l,r] This interval in the number of 1 is odd or even, ask the first few are correct, no contradiction
[L,r] The number of 1 can be expressed as sum[r]-sum[l-1], and the topic is only a parity, also determine the sum[r] and sum[l-1] parity is the same, to this step is simple, to change to the classic model
Range is a bit large, according to L-1,r Discretization
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <string > #include <cmath> #include <set> #include <queue> #include <algorithm> #include <vector > #include <stack> #include <map>using namespace std, #define ESP 1e-8const double PI = ACOs ( -1.0); const LONG Long inf = 1000000000;const long Long mod = 10000007;typedef long long LL; #pragma comment (linker, "/stack:1024000000,1024 000000 ")//freopen (" In.txt "," R ", stdin); input redirection, input data will be read from the In.txt file//freopen ("OUT.txt", "w", stdout); Output redirect, output data will be saved in out.txt file int f[30005];int v[30005];int a[30005], B[30005];char s[30005][10];set<int>st;set <int>::iterator It;map<int, int>mp;int find (int x) {if (x! = F[x]) {int u = f[x];f[x] = find (f[x]); v[x] = (V[u] + v[x])% 2;} return f[x];} int main () {int n, m, I, J;while (~scanf ("%d%d", &n, &m)) {int ans = 0;int res = 0;st.clear (); Mp.clear (); for (i = 1; I <= m; ++i) {scanf ("%d%d%s", &a[i], &amP;b[i], s[i]); St.insert (A[i]); St.insert (B[i]);} int nn = 0;for (it = St.begin (); It! = St.end (); ++it) {mp[*it] = ++nn;} for (i = 0; I <= nn; ++i) f[i] = I;memset (V, 0, sizeof (v)), v[0] = 0;for (i = 1; I <= m; ++i) {int fa = find (Mp[a[i])- 1); int fb = find (Mp[b[i]); if (FA = = FB)//If the same, then already know the Mp[a[i]]-1 and mp[b[i] "parity, so direct judgment can {if (V[mp[a[i]]-1] = = V[mp[b[i]] && s[i][0] = = ' O ') break;if (V[mp[a[i] [1]! = V[mp[b[i]] [&& s[i][0] = = ' E ') break;ans++;} ELSE{F[FA] = fb;if (s[i][0] = = ' O ')//If it is odd, need to add 1{v[fa] = (V[mp[a[i] [1] + v[mp[b[i]]] + 1)% 2;} ELSE{V[FA] = (V[mp[a[i] [1] + v[mp[b[i]]])% 2; ans++;}} printf ("%d\n", ans);}}
POJ 1733Parity Game (map discrete + take right and check set)