码迷,mamicode.com
首页 > 其他好文 > 详细

Gym100096D

时间:2017-08-11 00:27:32      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:pair   for   sep   回忆   scanf   tar   pre   pen   sam   

Problem D.

Guessing game Input file: game.in Output file: game.out

Byteman is playing a following game with Bitman. Bitman writes down some 1 000 000 000-element sequence of zeros and ones. Byteman’s task is to guess the sequence that Bitman created. He can achieve this goal by asking Bitman questions of the form: ’What is the parity of the sum of a subsequence of your sequence, that begins at the b-th element and ends at the e-th element of the sequence?’.

After playing the game for some time, Byteman started suspecting that Bitman was cheating. He would like to know at which moment did Bitman first answer his question in an inconsistent way, so he asked you for help. Write a program which: ? reads a description of Byteman’s questions and Bitman’s answers, ? computes the greatest number N, for which Bitman’s answers for the first N questions are consistent.

Input The first line of the input file contains one integer n (0 ≤ n ≤ 100 000), denoting the number of Byteman’s questions.Each of the following n lines describes one Byteman’s question with corresponding Bitman’s answer in the form of three positive integers b, e and s (1 ≤ b ≤ e ≤ 1 000 000 000, s ∈ {0, 1}), separated by single spaces. b and e are the indices of the first and the last element of the subsequence in Byteman’s question. s = 0 means that Bitman answered that the sum was even and s = 1 — that it was odd.

Output The first and only line of the output file should contain one integer — the greatest value of N such that there exists a sequence of zeros and ones that is consistent with Bitman’s answers to first N Byteman’s questions. Example game.in game.out 5 3 3 0 2 5 1 1 4 0 2 5 0 1 5 1 3 An example of a sequence consistent with three first Byteman’s questions from the sample input is 01011.

 

题目分析:场上没有想到用并查集,赛后回忆起了之前的一道权值并查集,如果只有两个种类,那么复制一个点也可以。

如果 sum[r]-sum[l-1]=0 那么sum[r]为偶数时 sum[l-1]为偶数 sum[r]为奇数时,sum[l-1]为奇数。

同理: sum[r]-sum[l-1]=1时 sum[r]和sum[l-1]奇偶性相反。

那么对每一个sum[i]建两个点 一个表示为奇数 一个表示为偶数,自然就用并查集维护了。

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define Pii pair<int,int>
 4 #define len 100001
 5 using namespace std;
 6 map<LL,int> mmp;
 7 int cnt=0,n,xx,yy,nx,ny,s,e,d;
 8 LL l,r;
 9 int f[2*len];
10 int find(int x){
11     return x==f[x]?x:f[x]=find(f[x]);
12 }
13 void Union(int x,int y){
14     if(x>y) f[x]=y;
15     else f[y]=x;
16 }
17 int main(){
18     freopen("game.in","r",stdin);
19     freopen("game.out","w",stdout);
20     cin>>n;
21     for(int i=0;i<=2*len;i++){
22         f[i]=i;
23     }
24     for(int i=1;i<=n;i++){
25         scanf("%I64d%I64d%d",&l,&r,&s);
26         l--;
27         if(mmp.find(l)==mmp.end()){
28             mmp[l]=++cnt;
29         }
30         if(mmp.find(r)==mmp.end()){
31             mmp[r]=++cnt;
32         }
33         
34         xx=find(mmp[l]);
35         yy=find(mmp[r]);
36         nx=find(mmp[l]+len);
37         ny=find(mmp[r]+len);
38         
39         if(s==0){
40             if(xx==ny||yy==nx){
41                 cout<<i-1<<endl;
42                 return 0;
43             }
44             Union(xx,yy);
45             Union(nx,ny);
46         }
47         else{
48             if(xx==yy||nx==ny){
49                 cout<<i-1<<endl;
50                 return 0;
51             }
52             Union(xx,ny);
53             Union(yy,nx);
54         }
55     }
56     cout<<n;
57     return 0;
58 }

 

Gym100096D

标签:pair   for   sep   回忆   scanf   tar   pre   pen   sam   

原文地址:http://www.cnblogs.com/poler/p/7342530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!