欢迎使用CSDN-markdown编辑器

POJ 3484 Showstopper
来源:POJ 3484 Showstopper
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
题目大意:求一组等差数列中,某个数(在所有数字中出现次数为奇数的数)出现的次数(当然是奇数次)并且把这个数找出来,输出来,ps(题目输入的时候能够保证出现奇数次的数只有一个,这个不用多加考虑)
思路:这题主要的考点是二分法,其中的mid就是出现次数为奇数的那个数,在这里有个前缀和(就是在所有等差数列中出现的指定的那个数的下标和),我在这里用了函数cal(k)来表示数字k的所有前缀和,即数字k出现的那个数的下标和,打个比方说就是如果a[i]==k的话,那么就是所有的i的和,因为最后只有一个出现次数为奇数的数,那么从前往后一定就是出现次数为偶偶偶偶偶偶奇,也就是在出现次数为奇数之前的绝对都是偶数次,所以只要cal[k]是奇数,那么就代表最后的答案一定就是在k的前边,这个时候right=mid,否则那么就一定就是在k后边,这个时候就是left=mid+1,在这里补充一点,为什么前缀和为奇数就是在前边的呢,因为如果为奇数,那么就代表那个组成这个前缀和的那些前缀里边就一定有前缀为奇数,根据马尔定理可得最后结论。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstdio>
#include <algorithm>
#define N 500005
typedef long long LL;
using namespace std;
int n ;
LL X[N] , Y[N] , Z[N];
char str[55];
LL cal(LL k)
{
  LL sum = 0 , x;
  for (int i = 1 ; i <= n; ++ i)
  {
    if (k < X[i]) continue;
    x = min(k , Y[i]);
    sum += (x - X[i]) / Z[i] + 1;
  }
  return sum;
}
void work()
{
  n = 1;
  X[n] = 0;
  sscanf(str , "%I64d %I64d %I64d" , &X[n] , &Y[n] , &Z[n]);
  if (!X[n]) return;
  memset(str , 0 , sizeof(str));
  while (gets(str) , *str)
    ++ n , sscanf(str , "%I64d %I64d %I64d" , &X[n] , &Y[n] , &Z[n]) , memset(str , 0 , sizeof(str));
  LL l = 1 , r = 1LL << 33 , m;
  while (l < r)
  {
    m = (l + r) >> 1;
    if (cal(m) & 1)
      r = m;
    else l = m + 1;
  }
  if (l == 1LL << 33)
    puts("no corruption");
  else printf("%I64d %I64d\n" , l , (cal(r) - cal(r - 1)));
}

int main()
{
  while(gets(str))
    work();
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值