现在的位置: 首页 > 综合 > 正文

Timus 1003. Parity

2012年05月13日 ⁄ 综合 ⁄ 共 5460字 ⁄ 字号 评论关闭
文章目录
Timus 1003. Parity 是和奇偶校验相关的题目。

1003. Parity

Time Limit: 2.0 second
Memory Limit: 16 MB
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 him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask 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 not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

Input

Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1 000 000 000. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. The remaining lines specify questions and answers. Each line contains one question and the answer to this 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 in the chosen subsequence, where “even” means an even number of ones and “odd” means an odd number). The file is ended with a line containing −1.

Output

Each line of output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying 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 be the number of all the questions asked.

Sample

input output
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1
3

Problem Source: Central European Olympiad in Informatics 1999


解答如下:
 1 using System;
 2 using System.IO;
 3 using System.Collections.Generic;
 4 
 5 namespace Skyiv.Ben.Timus
 6 {
 7   // http://acm.timus.ru/problem.aspx?space=1&num=1003
 8   sealed class T1003
 9   {
10     struct Tuple<T1, T2>
11     {
12       public T1 A;
13       public T2 B;
14       public Tuple(T1 a, T2 b) { A = a; B = b; }
15     }
16     
17     struct Tuple<T1, T2, T3>
18     {
19       public T1 A;
20       public T2 B;
21       public T3 C;
22     }
23     
24     static void Main()
25     {
26       new T1003().Run(Console.In, Console.Out);
27     }
28     
29     void Run(TextReader reader, TextWriter writer)
30     {
31       while (reader.ReadLine() != "-1") writer.WriteLine(Find(Read(reader)));
32     }
33     
34     Tuple<intintbool>[] Read(TextReader reader)
35     {
36       Tuple<int,int,bool>[] ts = new Tuple<int,int,bool>[int.Parse(reader.ReadLine())];
37       for (int i = 0; i < ts.Length; i++)
38       {
39         string[] ss = reader.ReadLine().Split();
40         ts[i].A = int.Parse(ss[0]) - 1;
41         ts[i].B = int.Parse(ss[1]);
42         ts[i].C = (ss[2== "odd");
43       }
44       return ts;
45     }
46     
47     int Find(Tuple<intintbool>[] ts)
48     {
49       int[] q = new int[ts.Length * 2];
50       for (int i = 0; i < ts.Length; i++)
51       {
52         q[i * 2= ts[i].A;
53         q[i * 2 + 1= ts[i].B;
54       }
55       Distinct(ref q);
56       Array.Sort(q);
57       Tuple<intbool>[] tree = new Tuple<int,bool>[q.Length];
58       for (int i = 0; i < tree.Length; i++) tree[i].A = i;
59       for (int i = 0; i < ts.Length; i++)
60       {
61         int n = Array.BinarySearch(q, ts[i].B);
62         Tuple<intbool> t2 = FindRoot(tree, n);
63         Tuple<intbool> t1 = FindRoot(tree, Array.BinarySearch(q, ts[i].A));
64         if (t1.A != t2.A) tree[t1.A] = new Tuple<int,bool>(n, t1.B ^ ts[i].C);
65         else if (t1.B ^ t2.B ^ ts[i].C) return i;
66       }
67       return ts.Length;
68     }
69     
70     Tuple<intbool> FindRoot(Tuple<intbool>[] tree, int n)
71     {
72       Tuple<intbool> result = new Tuple<intbool>();
73       for (; n != tree[n].A; n = tree[n].A) result.B ^= tree[n].B;
74       result.A = n;
75       return result;
76     }
77     
78     void Distinct<T>(ref T[] array)
79     {
80       Dictionary<T, object> dict = new Dictionary<T, object>();
81       foreach (T v in array) dict[v] = null;
82       dict.Keys.CopyTo(array, 0);
83       Array.Resize(ref array, dict.Count);
84     }
85   }
86 }
87 /* 
88 Got Ac by using serious algorithm of building spanning forest 
89 in bipartite graph of segment’s ends only.
90 All simple “sport’s” attempts are failed because of TLE.
91 Fist mistake in answers found when meet chord [a,b] in some 
92 tree of the forest but dist[a]+dist[b]+ves([a,b]) is odd;
93 Dist[]- Boolean function of distances from roots of trees to their nodes.
94 Basic operation- link of two trees when new arc [a,b] connects to different trees.
95 For each node v tree[v]- tree, containing v- helpful array.
96 For linking trees used vector<int>Smeg[5000]- as dynamic structure of the Graph.
97 And BFS when second tree linking to the first one.
98 */

 

这道题是说,你和你的朋友玩一个游戏。你的朋友写下一个只包含“0”和“1”的序列,而你选择一个其中的一个子序列问你的朋友,该子序列中“1”的数目是奇数还是偶数?你的朋友回答后,你可以再选择一个子序列继续提问。但是你怀疑你的朋友的答案不全是正确的。因此你决定写一个程序来证实这一点。

程序的输入包含一系列案例。每个案例的第一行包含一个数,指示该序列的长度(不大于十亿)。第二行是一个不大于五千的非负整数,表示你提问的次数。剩余的行代表你的提问和你朋友的回答,每行由两个整数(分别表示子序列的开始和结束位置)和一个英文单词(“even”或“odd”,表示该子序列的奇偶性)构成。输入文件的最后一行包含“-1”。

对于每个案例,程序都在单独一行输出一个数,表示开头有多少个回答是正确的。

上述程序第 34 至 45 行的 Read 方法读取输入。然后调用第 47 至 68 行的 Find 方法寻找答案。第 78 至 84 行的 Distinct 方法用于删除数组中的重复元素。


返回目录

抱歉!评论已关闭.