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

(具体的可以自己验证)

通过上面的规律,就可以通过二分搜索来求得J了

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 50010
typedef long long ll;
ll X[maxn], Y[maxn], Z[maxn];
int cnt;
char str[maxn];

ll judge(ll mid) {
    ll Sum = 0;
    for(int i = 0; i < cnt; i++) {
        if(mid < X[i])
            continue;
        ll t = min(mid, Y[i]);
        Sum += (t - X[i])/ Z[i] + 1;
    }
    return Sum;
}

void solve() {
    cnt = 1;
    X[0] = 0;
    sscanf(str,"%lld%lld%lld", &X[0], &Y[0], &Z[0]);
    if(!X[0])
        return ;

    while(gets(str) && str[0]) {
        sscanf(str,"%lld%lld%lld", &X[cnt], &Y[cnt], &Z[cnt]);
        cnt++;
    }

    ll l = 0, r = 1LL << 32;
    while(l < r) {
        ll mid = (l + r) / 2;
        if(judge(mid) % 2)
            r = mid;
        else
            l = mid + 1;
    }
    if(l == (1LL << 32))
        printf("no corruption\n");
    else
        printf("%lld %lld\n",l, judge(l) - judge(l - 1));
}

int main() {
    while(gets(str))
        solve();
    return 0;
}
时间: 2024-10-26 00:26:24

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 3484 Showstopper(二分答案)

[题目链接] http://poj.org/problem?id=3484 [题目大意] 给出n个等差数列的首项末项和公差.求在数列中出现奇数次的数.题目保证至多只有一个数符合要求. [题解] 因为只有一个数符合要求,所以在数列中数出现次数的前缀和必定有奇偶分界线, 所以我们二分答案,计算前缀和的奇偶性进行判断,得到该数的位置. [代码] #include <cstdio> #include <algorithm> #include <cstring> using na

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 1064 1759 3484 3061 (二分搜索)

POJ 1064 题意 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留小数点后2位. 思路 二分搜索.这里要注意精度问题,代码中有详细说明:还有printf的%.2f会四舍五入的,需要*100再取整以截取小数点后两位. #include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<math

二分题目总结

在UVA上搜索二分时搜到了一个很好的public专题 1.POJ - 3258 River Hopscotch http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16277 解题思路:http://blog.csdn.net/l123012013048/article/details/45646911 这题是符合条件的情况下,解有可能是偏小的 2.POJ - 3273 Monthly Expense http://acm.hu

POJ 1064 Cable master(很好玩的二分搜索)

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24292   Accepted: 5200 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

poj 3579 Median (二分搜索之查找第k大的值)

Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numbers: ∣Xi - Xj∣ (1 ≤ i < j ≤ N). We can get C(N,2) differences through this work, and now your task is to find the median of the differences as quickly

二分搜索 POJ 1064 Cable master

题目传送门 1 /* 2 题意:n条绳子问切割k条长度相等的最长长度 3 二分搜索:搜索长度,判断能否有k条长度相等的绳子 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e4 + 10; 12 const int INF = 0x3f3f

二分搜索 POJ 2456 Aggressive cows

题目传送门 1 /* 2 二分搜索:搜索安排最近牛的距离不小于d 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 using namespace std; 8 9 const int MAXN = 1e5 + 10; 10 const int INF = 0x3f3f3f3f; 11 int x[MAXN]; 12 int n, m; 13 14 bool check(int d)