Poj3484-Showstopper(二分脑洞题)

Description

Data-mining huge data sets can be a painful and long lasting process if we are not aware of tiny patterns existing within those data sets.

One reputable company has recently discovered a tiny bug in their hardware video processing solution and they are trying to create software workaround. To achieve maximum performance they use their chips in pairs and all data objects in memory should have even number of references. Under certain circumstances this rule became violated and exactly one data object is referred by odd number of references. They are ready to launch product and this is the only showstopper they have. They need YOU to help them resolve this critical issue in most efficient way.

Can you help them?

Input

Input file consists from multiple data sets separated by one or more empty lines.

Each data set represents a sequence of 32-bit (positive) integers (references) which are stored in compressed way.

Each line of input set consists from three single space separated 32-bit (positive) integers X Y Z and they represent following sequence of references: X, X+Z, X+2*Z, X+3*Z, …, X+K*Z, …(while (X+K*Z)<=Y).

Your task is to data-mine input data and for each set determine weather data were corrupted, which reference is occurring odd number of times, and count that reference.

Output

For each input data set you should print to standard output new line of text with either “no corruption” (low case) or two integers separated by single space (first one is reference that occurs odd number of times and second one is count of that reference).

Sample Input

1 10 1
2 10 1

1 10 1
1 10 1

1 10 1
4 4 1
1 5 1
6 10 1

Sample Output

1 1
no corruption
4 3

题意:每组测试样例会给出多组X,Y,Z的值,代表X,X+Z,X+2*Z....X+k*Z(X+k*Z<=Y)这些值会出现一次。然后题目保证所有出现的数的次数要么全是偶数个,要么只有一个数的次数是奇数个,
如果都是偶数个,则输出no corruption,否则输出这个数以及出现的次数。

解析:这题X,Y,Z的值这么大,显然不可能枚举,请注意题目给的条件,仔细想想后,可以用二分来做,先判断所有出现的数的次数之和是否是偶数,如果是,直接输出no corruption,因为是
偶数的话不可能出现某个数出现的次数是奇数的。然后再二分,计算小于等于mid的所有数出现的次数,如果为偶数则向大的走,否则向小的走(仔细思考一下就能明白)。还有这道题的输入比较
坑,注意一下。

代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
typedef __int64 LL;
const LL INF=1e10+7;
const double eps=1e-7;
const int maxn=100005;
int N;
struct node
{
    LL x,y,z;
    node(LL x=0,LL y=0,LL z=0):x(x),y(y),z(z){}
}nod[maxn];
bool judge(LL mid)
{
    LL ret=0;
    for(int i=0;i<N;i++)
    {
        node& t=nod[i];
        LL x=t.x,y=t.y,z=t.z;
        if(mid<x) continue;
        LL a=min(y,mid);
        ret+=(a-x)/z+1;
    }
    return ret%2==0;
}
LL Cal(LL v)
{
    LL ret=0;
    for(int i=0;i<N;i++)
    {
        node& t=nod[i];
        LL x=t.x,y=t.y,z=t.z;
        if(v<x||v>y) continue;
        if((v-x)%z==0) ret++;
    }
    return ret;
}
void solve()
{
    LL ret=0;
    for(int i=0;i<N;i++) //计算整个值
    {
        node& t=nod[i];
        LL x=t.x,y=t.y,z=t.z;
        ret+=(y-x)/z+1;
    }
    if(ret%2==0){ cout<<"no corruption"<<endl; return; } //为偶数
    LL low=0,up=INF,mid;
    while(true)
    {
        if(low==up) break;
        mid=(low+up)/2;
        if(judge(mid)) low=mid+1;   //二分
        else up=mid;
    }
    printf("%I64d %I64d\n",low,Cal(low));
}
int main()
{
    char S[50];
    bool err=false;
    while(true)
    {
        N=0;
        while(true)
        {
            if(gets(S)==NULL) return 0;
            if(strlen(S)==0) continue;
            break;
        }
        LL x,y,z;
        string s="";
        for(int i=0;S[i]!='\0';i++) s+=S[i];
        istringstream out(s);//字符串输入流
        out>>x>>y>>z;
        if(x<=y) nod[N++]=node(x,y,z);
        while(true)
        {
            if(gets(S)==NULL) {err=true; break; }
            if(strlen(S)==0) break;
            string s="";
            for(int i=0;S[i]!='\0';i++) s+=S[i];
            istringstream out(s);
            out>>x>>y>>z;
            if(x<=y) nod[N++]=node(x,y,z);
        }
        solve();
        if(err) break;
    }
    return 0;
}
View Code

 

posted @ 2016-07-11 14:09  wust_ouyangli  阅读(205)  评论(0编辑  收藏  举报