欢迎使用优快云-markdown编辑器

本文详细解析了POJ3484Showstopper问题,通过二分查找法解决了一组等差数列中某个数出现奇数次的问题,并给出了完整的C++代码实现。

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;
}
源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值