POJ 3484(规律+二分)

来源:互联网 发布:哪些犯罪知乎 编辑:程序博客网 时间:2024/05/22 11:31

Showstopper
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

Submit Status

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 12 10 11 10 11 10 11 10 14 4 11 5 16 10 1

Sample Output

1 1no corruption4 3

只有通过递推发现规律,才能想到二分的做法

题目大意:给出N个X Y Z组合,其中X Y Z组合能够输出 X, X + Z, X + 2 * Z… X + K * Z(X+K * Z <= Y)问这些输出的数中,有哪个数是输出奇数次的

解题思路:输出保证最多只有一个奇数 
假设J是输出奇数次的那个数,那么小于J的所有输出的数的个数之和就为偶数,大于等于J的所有输出的数的个数之和为奇数 
如果以i为标准,输出小于等于i的所有数之和,i从小到大变化的话,就会有如下的形式

 
偶偶偶偶偶偶奇奇奇。。。第一个奇刚好是J (只有推导出这个规律,才能联想用二分暴搜)


(具体的可以自己验证) 
通过上面的规律,就可以通过二分搜索来求得J了


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 51000;
LL x[N], y[N], z[N];
char str[N];
void solve();
LL judge(LL s);
int k;


int main()
{
    while(gets(str))
    {
        solve();
    }
    return 0;
}


void solve()
{
    x[0]=0;
    sscanf(str,"%lld %lld %lld",&x[0], &y[0], &z[0]);
    if(x[0]==0)
    {
        return ;
    }
    k=1;
    while(gets(str)&&str[0])
    {
        sscanf(str,"%lld %lld %lld",&x[k], &y[k], &z[k]);
        k++;
    }
    LL l=0, r=1LL<<32,ans=1LL<<32, mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(judge(mid)%2==1)
        {
            ans=mid;
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }
    if(ans==(1LL<<32))
    {
        printf("no corruption\n");
    }
    else
    {
        printf("%lld %lld\n",ans, judge(ans)-judge(ans-1));(暴力查找,避免数据丢失);
    }
    return ;
}




LL judge(LL s)
{
    LL cnt=0;
    for(int i=0;i<k;i++)
    {
        if(s>y[i])
        {
            if(x[i]<=y[i])
            {
                cnt+=(1+(y[i]-x[i])/z[i]);
            }
        }
        else
        {
            if(s>=x[i])
            {
                cnt+=(1+(s-x[i])/z[i]);
            }
        }
    }
    return cnt;
}

0 0
原创粉丝点击