POJ3484_Showstopper_前缀和思想&&二分

来源:互联网 发布:淘宝联盟手机版5.2苹果 编辑:程序博客网 时间:2024/06/03 13:34

Showstopper
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2138 Accepted: 644

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


给出若干个数列,在所有这些数列中,大部分元素都出现了偶数次,最多只有一个元素出现了奇数次。若存在一个出现了奇数次的元素,输出这个元素和它出现的次数。如果不存在,输出no corruption。其中每一个数列以X Y Z 的形式给出,X, X+Z, X+2*Z, X+3*Z, …, X+K*Z, …(while (X+K*Z)<=Y). 


这里有一个把问题转化为前缀和的技巧。假设出现了次的这个数为 O,则O之前的前缀和都是偶数,O之后的前缀和都是基数。因此可以用二分的方法做。


#include<cstdio>#include<iostream>#include<cstring>using namespace std;typedef long long ll;const ll inf = 0x8f8f8f8f8f;const ll maxn = 100000;char s[100];int squ;ll X[maxn], Y[maxn], Z[maxn];ll Getsum(ll x){ll sum = 0;for(int i= 0; i< squ; i++){if(x < X[i]) continue;sum += (min(Y[i], x) - X[i]) / Z[i] + 1;}return sum;}bool Judge(ll x){ll sum = Getsum(x);if(sum & 1 == 1) return true;return false;}void solve (){ll lb = 1, ub = inf;ll ans = -1;while(lb <= ub){ll md = (ub + lb) >> 1;if(Judge(md)) ans = md, ub = md - 1;else lb = md + 1;}if(ans == -1) cout << "no corruption\n";else cout << ans << " " << Getsum(ans) - Getsum(ans-1) << endl;}int main (){while(gets(s) != NULL){if(strlen(s) == 0){if(squ == 0) continue;solve();squ = 0;}else{sscanf(s, "%lld %lld %lld", X+squ, Y+squ, Z+squ);//敲黑板squ ++;}}if(squ != 0) solve();return 0;}


0 0
原创粉丝点击