题目链接
思维题 参考感谢大神博客
描述:
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 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.
输出:
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).
样例输入:
1 10 1
2 10 1
1 10 1
1 10 1
1 10 1
4 4 1
1 5 1
6 10 1
样例输出:
1 1
no corruption
4 3
个人觉得这题还是非常不错的题拉。
然而poj今天炸了,题目还没交,但是思路也就是这个思路拉。想写完题目就顺便把博客写了吧。不想拖到明天了,lll¬ω¬)。
题意就是:给出N组X,Y,Z。X,X+Z,X+2*Z,X+3*Z (X+K*Z <= Y) 求这样的N组数中,出现次数为奇数次的数字。输入保证最多只有一个数字出现的次数为奇数。例如输入:
1 10 1 ,1 10 1. 则1到10分别出先了两次没有一个数字出现的次数是奇数次,所以输出“no corruption” 当输入1 10 1,2 10 1 时,1这个数字出现的次数是1次 所以输出1,以及他的次数1.
所以题意就非常清楚了。
思路:
设C_0,C_1,C_2,C_n为 所有的C0 ,C1,…,CN出现的次数,T_i = C_0+C_1+…+C_n ,如果C_x出现的次数是奇数次的化 ,那么对于所有的(i>x) T_i 都是奇数,否则都是偶数。这就是二分的单调性。 一定是这样的,因为题目保证最多只有一个数字出现的次数是奇数。
poj炸了,并未ac 差不多就这样
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define INF 1e18
typedef long long ll;
using namespace std;
const int maxn = 1e6+5;
char s[1024];
ll a[maxn],b[maxn],c[maxn];
ll d[maxn];
ll ans;
ll C(ll mid){
ll sum = 0 ;
for(int i=0;i<ans;i++){
if(mid >= b[i]){
sum+=d[i];
}else if(mid>=a[i]){
sum+=((mid-a[i])/c[i]+1);
}
}
return sum;
}
void solve(){
ll lowbit = 0;
for(int i=0;i<ans;i++){
d[i] = (b[i]-a[i])/c[i]+1;
lowbit = lowbit ^ d[i];
}
if(!(lowbit & 1)){
printf("no corruption\n");
return;
}
ll l = 0,r = INF;
while(r -l>1){
ll mid = (r+l)/2;
if(C(mid) & 1){
r = mid;
}else{
l = mid;
}
}
printf("%I64d %I64d\n",r,C(r) -C(r-1));
}
int main(){
ans = 0;
while(gets(s)){
if(strlen(s) == 0){
if(ans == 0) continue;
else{
solve();
ans = 0;
}
}else{
sscanf(s,"%I64d %I64d %I64d",&a[ans],&b[ans],&c[ans]);
ans++;
}
}
if(ans){
solve(); //注意可能最后一组输入没有空格了。这个地方是个深坑。
}
return 0;
}