POJ 3484(规律+二分)

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

 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 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

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

题目大意:给出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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值