POJ 3484 Showstopper(二分答案)

【题目链接】 http://poj.org/problem?id=3484

【题目大意】

  给出n个等差数列的首项末项和公差。求在数列中出现奇数次的数。题目保证至多只有一个数符合要求。

【题解】

  因为只有一个数符合要求,所以在数列中数出现次数的前缀和必定有奇偶分界线,

  所以我们二分答案,计算前缀和的奇偶性进行判断,得到该数的位置。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=500010;
const LL inf=1LL<<33;
LL X[N],Y[N],Z[N];
char s[60];
int n;
LL cal(LL x){
    LL ans=0,t;
    for(int i=1;i<=n;i++){
        if(x<X[i])continue;
        t=min(x,Y[i]);
        ans+=(t-X[i])/Z[i]+1;
    }return ans;
}
int main(){
    while(gets(s)){
        X[n=1]=0;
        sscanf(s,"%lld %lld %lld",&X[n],&Y[n],&Z[n]);
        if(!X[n])continue;
        memset(s,0,sizeof(s));
        while(gets(s),*s)n++,sscanf(s,"%lld %lld %lld",&X[n],&Y[n],&Z[n]),memset(s,0,sizeof(s));
        LL l=1,r=inf,ans=0;
        while(l<=r){
            LL mid=(l+r)>>1;
            if(cal(mid)&1LL)r=mid-1,ans=mid;
            else l=mid+1;
        }if(!ans)puts("no corruption");
        else printf("%lld %lld\n",ans,cal(ans)-cal(ans-1));
    }return 0;
}

  

时间: 2024-08-07 02:44:19

POJ 3484 Showstopper(二分答案)的相关文章

POJ 3484 Showstopper 二分

 Showstopper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1218   Accepted: 356 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. On

POJ 3122 Pie 二分答案

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9653   Accepted: 3478   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of v

POJ 3104 Drying(二分答案)

题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11128   Accepted: 2865 Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afra

poj 3484 Showstopper

Showstopper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2057   Accepted: 612 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 r

POJ - 3484 Showstopper 二分搜索

题目大意:给出N个X Y Z组合,其中X Y Z组合能够输出 X, X + Z, X + 2 * Z- X + K * Z(X+K * Z <= Y)问这些输出的数中,有哪个数是输出奇数次的 解题思路:输出保证最多只有一个奇数 假设J是输出奇数次的那个数,那么小于J的所有输出的数的个数之和就为偶数,大于等于J的所有输出的数的个数之和为奇数 如果以i为标准,输出小于等于i的所有数之和,i从小到大变化的话,就会有如下的形式 偶偶偶偶偶偶奇奇奇...第一个奇刚好是J (具体的可以自己验证) 通过上面的

POJ 3080 Blue Jeans(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通过拼接符拼成一个串,做一遍后缀数组,二分答案,对于二分所得值,将h数组大于这个值的相邻元素分为一组,判断组内元素是否覆盖全字典,是则答案成立,对于答案扫描sa,输出第一个扫描到的子串即可. [代码] #include <cstdio> #include <cstring> #inclu

字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在学习后缀数组的时候已经做过一遍了,但是现在主攻字符串hash,再用字符串hash写一遍. 这题的思路是这样的: 1)取较短的串的长度作为high,然后二分答案(每次判断长度为mid=(low+high)>>1是否存在,如果存在就增加下界:不存在就缩小上界): 2)主要是对答案的判断(judge函数

POJ 3294 Life Forms(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3294 [题目大意] 求出在至少在一半字符串中出现的最长子串. 如果有多个符合的答案,请按照字典序输出. [题解] 将所有的字符串通过不同的拼接符相连,作一次后缀数组, 二分答案的长度,然后在h数组中分组,判断是否可行, 按照sa扫描输出长度为L的答案即可.注意在一个子串中重复出现答案串的情况. [代码] #include <cstdio> #include <cstring> #include <vecto

POJ 3662 Telephone Lines(二分答案+SPFA)

[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求最小费用. [题解] 二分答案,对于大于二分答案的边权置为1,小于等于的置为0, 则最短路就是超出二分答案的线数,如果小于等于k,则答案是合法的 [代码] #include <cstdio> #include <cstring> using namespace std; const i