POJ 3484 Showstopper (二分搜索)

Showstopper
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1429   Accepted: 422

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

嘛 - - 32位正数 不知道是不是无符号 所以还是用了long long 读入有点微坑 注意处理case间多个空格的情况 标记下有没有数据就好了

假设C_i 是i 第i个数的个数  由于只有存在唯一个奇数个数的数  那么我们可以利用前缀和单调来二分

比如Sum_i是前缀和  假设第2个数是奇数个数   一共5个数其他的都是偶数个数

Sum_1 = C_1  偶数

Sum_2 = C_1 + C_2  偶数+奇数 = 奇数

Sum_3 = C_1 + C_2 + C_3  奇数+偶数 = 奇数

Sum_4 = C_1 + C_2 + C_3 + C_4 奇数+偶数 = 奇数

Sum_5 = C_1 + C_2 + C_3 + C_4 + C_5 奇数+偶数 = 奇数

所以就是从那个奇数个数的开始都是奇数  存在 偶偶偶奇奇奇奇奇奇奇  这样的序列 就可以二分了找到第一个奇就是ans

然后不知道为啥闭区间和开区间的二分都写死循环 只有半开半闭的这种过了 - -


AC代码如下:

//
//  Created by TaoSama on 2015-04-30
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n;
vector<long long> x, y, z;
char a[100];

bool read() {
    x.clear(); y.clear(); z.clear();
    bool have = false;
    while(gets(a)) {
        if(!strlen(a)) {
            if(have) break;
            else continue;
        }
        long long xx, yy, zz;
        sscanf(a, "%lld%lld%lld", &xx, &yy, &zz);
        have = true;
        x.push_back(xx);
        y.push_back(yy);
        z.push_back(zz);
    }
    n = x.size();
    return have;
}

long long calc(long long mid) {
    long long sum = 0;
    for(int i = 0; i < n; ++i) {
        if(mid < x[i]) continue;
        long long t = min(mid, y[i]);
        sum += (t - x[i]) / z[i] + 1;
    }
    //cout<<sum<<endl;
    return sum;
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(read()) {
        /*for(int i = 0; i < n; ++i) {
            cout << x[i] << ' ' << y[i] << ' ' << z[i] << endl;
        }
        cout << endl;*/

        long long l = 1, r = 1LL << 33;
        while(l < r) {
            long long mid = l + (r - l >> 1);
            if(calc(mid) & 1) r = mid;
            else l = mid + 1;
        }

        if(l == 1LL << 33) printf("no corruption\n");
        else printf("%lld %lld\n", l, calc(l) - calc(l - 1));
    }
    return 0;
}


你可能感兴趣的:(POJ 3484 Showstopper (二分搜索))