[关闭]
@xiaoziyao 2020-03-26T12:40:05.000000Z 字数 3157 阅读 1275

Timus Online Judge(Ural)1003 Parity题解

Timus


题目简述:5/4/4/5/离散化+并查集

传送门:1003. Parity

题面

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 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 109. 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 input is ended with a line containing −1.

输出

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.

样例

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

分析

题意:(有多组数据)给定一个数),表示范围和)次操作,每次操作:l r even/odd表示数字之和为奇数/偶数,但是给定的操作中可能有错误的,找到第一个错误操作的位置,输出这个操作的编号减,否则输出

这道题思维比较妙,首先要将和转换为前缀和之差,这一点很显然

其次,可以把前缀和之差变成并查集维护的两个集合,集合相同的点奇偶性相同,集合不同的点奇偶性不同,然后就是套路了。

注意,这道题要离散化,因为范围()太大了,但询问最多会产生个点,所以是显然的离散化

代码

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. const int maxn=100005;
  6. int i,j,k,m,n,nums,tot,flg;
  7. int a[maxn][3],b[maxn],f[maxn],d[maxn];
  8. string s;
  9. struct number{
  10. int val,pos,mrk;
  11. }num[maxn];
  12. inline int cmp(number a,number b){
  13. return a.val<b.val;
  14. }
  15. int find(int x){
  16. if(f[x]==x)
  17. return x;
  18. int res=find(f[x]);
  19. d[x]^=d[f[x]];
  20. f[x]=res;
  21. return res;
  22. }
  23. int merge(int x,int y,int z){
  24. int a=find(x),b=find(y);
  25. if(a==b)
  26. return d[x]^d[y]==z;
  27. f[a]=b;
  28. d[a]=d[x]^d[y]^z;
  29. return 1;
  30. }
  31. int main(){
  32. while(1){
  33. scanf("%d",&n);
  34. if(n==-1)
  35. break;
  36. scanf("%d",&m);
  37. nums=0;
  38. for(i=1;i<=m;i++){
  39. int x,y;
  40. scanf("%d%d",&x,&y);
  41. cin>>s;
  42. x--;
  43. if(s=="even")
  44. b[i]=0;
  45. else b[i]=1;
  46. num[++nums].val=x,num[nums].pos=i,num[nums].mrk=1;
  47. num[++nums].val=y,num[nums].pos=i,num[nums].mrk=2;
  48. }
  49. sort(num+1,num+1+nums,cmp);
  50. tot=1,a[num[1].pos][num[1].mrk]=tot;
  51. for(i=2;i<=nums;i++){
  52. if(num[i].val!=num[i-1].val)
  53. tot++;
  54. a[num[i].pos][num[i].mrk]=tot;
  55. }
  56. for(i=1;i<=tot;i++)
  57. f[i]=i,d[i]=0;
  58. flg=0;
  59. for(i=1;i<=m;i++)
  60. if(merge(a[i][1],a[i][2],b[i])==0){
  61. printf("%d\n",i-1);
  62. flg=1;
  63. break;
  64. }
  65. if(flg==0)
  66. printf("%d\n",m);
  67. }
  68. return 0;
  69. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注