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 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

题意:题目给出了多个等差数列的首项X_i,末项Y_i和公差Z_i,我们现在可以想象成把这些等差数列里的所有不同的数组成一个集合S,需要寻找集合S里的一个元素,这个元素在所有这些等差数列中总共出现过奇数次(题意知除该元素外其余的元素都出现偶数次)。思路:假若将集合S中的元素从小到大排列,用time[i]表示第i个元素在等差数列中出现的次数,再设元素j出现了奇数次,那么在元素j出现前, ∑time[i](0<=i<j)一定是偶数,若此时再加上time[j],和必定变为奇数,对此可用二分查找的方式找到元素j,而实际求∑time[i](0<=i<j)时,并不用知道具体每个元素出现多少次,只需要计算每个数列中比元素j小的元素的数量并累加求和即可。还有这题的输入很奇特,要注意!!!AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<sstream>
using namespace std;
const int N_MAX = 1000000+4;
typedef long long ll;
ll X[N_MAX], Y[N_MAX], Z[N_MAX];
ll num[N_MAX];//记录每个数列有几个数
int N;
char buf[1024];

ll C(const ll&x){//为了判断x出现次数是否奇数次,首先判断小于等于x的数总共是多少,如果是奇数,说明x可能太大,若全是偶数,则x一定太小
    ll sum = 0;
    for (int i = 0; i < N; i++) {
        if (Y[i] <= x)sum += num[i];
        else if (X[i] <= x)sum += (x - X[i]) / Z[i] + 1;
    }
    return sum;
}
bool input() {
    N = 0;
    bool what=0;
    while ((what=(gets_s(buf)!=NULL))&&strlen(buf)>2) {//主要是为了处理回车的操作
        sscanf(buf,"%lld%lld%lld",&X[N],&Y[N],&Z[N]);
        N++;
    }
    return what || N;//如果没有输入了并且N也为0,那么说明没有需要操作的数据了
}

int main() {
    while (input()) {
        if (!N)continue;//只输入空格就无视这种情况
        ll judge = 0;
        for (int i = 0; i < N;i++) {
            num[i] = (Y[i] - X[i]) / Z[i] + 1;
            judge += num[i];
        }
        if (!(judge & 1)) {//若所有的数的数量加起来为偶数,意味着没有异常元素
            printf("no corruption\n");
        }
        else {
            ll lb = 0,ub=INT_MAX;
            while (ub-lb>1) {
                ll mid = (lb+ub) >> 1;
                if (!(C(mid) & 1))lb = mid;
                else ub = mid;/////(lb,ub]
            }
            printf("%lld %lld\n",ub,C(ub)-C(ub-1));
        }
    }
    return 0;
}
 
				
时间: 2025-01-02 17:25:20

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 二分搜索

题目大意:给出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 (具体的可以自己验证) 通过上面的

二分题目总结

在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 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

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c