【HDOJ 4768】 Flyer (等差数列+二分)

【HDOJ 4768】 Flyer (等差数列+二分)

Flyer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2022    Accepted Submission(s): 743

Problem Description

The new semester begins! Different kinds of student societies are all trying to advertise themselves, by giving flyers to the students for introducing the society. However, due to the fund shortage, the flyers of a society can only be distributed to a part
of the students. There are too many, too many students in our university, labeled from 1 to 2^32. And there are totally N student societies, where the i-th society will deliver flyers to the students with label A_i, A_i+C_i,A_i+2*C_i,…A_i+k*C_i (A_i+k*C_i<=B_i,
A_i+(k+1)*C_i>B_i). We call a student "unlucky" if he/she gets odd pieces of flyers. Unfortunately, not everyone is lucky. Yet, no worries; there is at most one student who is unlucky. Could you help us find out who the unfortunate dude (if any) is? So that
we can comfort him by treating him to a big meal!

Input

There are multiple test cases. For each test case, the first line contains a number N (0 < N <= 20000) indicating the number of societies. Then for each of the following N lines, there are three non-negative integers A_i, B_i, C_i (smaller than 2^31, A_i <=
B_i) as stated above. Your program should proceed to the end of the file.

Output

For each test case, if there is no unlucky student, print "DC Qiang is unhappy." (excluding the quotation mark), in a single line. Otherwise print two integers, i.e., the label of the unlucky student and the number of flyers he/she gets, in a single line.

Sample Input

2
1 10 1
2 10 1
4
5 20 7
6 14 3
5 9 1
7 21 12

Sample Output

1 1
8 1

Source

2013 ACM/ICPC Asia Regional Changchun Online

Recommend

liuyiding

发。。超人? 。。翻译略过 反正是给小盆友发什么东西。。 然后发n次 每次规则是从A下标开始发 每C人发一个 发到B之前 即每次得到玩具的小朋友是A A+C A+2*C... A+K*C(A+K*C <= B)

问是否有得到奇数个玩具的小朋友 保证有的话只有一人 输出该小朋友编号和得到玩具数 没有输出-1

二分 所有组中最小编号为下界 最大B为上界 二分条件是所有组从A到mid(二分中点) 得到玩具的个数和为奇数时 则玩具在low~mid里 否则在mid~high里(有的话)

因为如果mid之前小朋友有一个得到奇数的话 之前所有小朋友得到玩具数一定为奇 否则为偶 (偶+偶得偶 偶+奇得奇数) 因为题目说了有的话只有一个奇数 如果有多个的话奇+奇得奇 就没法这么做了

还要注意的是上限2^31 爆ing

代码如下:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

typedef struct Range Range;

struct Range
{
    ll a,b,c;
};

Range rg[23333];
ll sum;

ll Binary(ll low,ll high,int n)
{
    if(sum%2 == 0) return -1;
    int i;
    ll cnt,mid,id;
    while(low <= high)
    {
        mid = (low+high)>>1;
        for(cnt = i = 0; i < n; ++i)
        {
            if(mid >= rg[i].a)
                cnt += (min(rg[i].b,mid)-rg[i].a)/rg[i].c+1;
        }
        if(cnt%2 == 1)
        {
            id = mid;
            high = mid-1;
        }
        else
            low = mid+1;
    }
    return id;
}

int main()
{
    int i,n;
    ll low,high,ans,cnt;
    while(~scanf("%d",&n))
    {
        low = 1LL<<31;
        high = 0;
        sum = 0;
        for(i = 0; i < n; ++i)
        {
            scanf("%I64d %I64d %I64d",&rg[i].a,&rg[i].b,&rg[i].c);
            high = max(high,rg[i].b);
            low = min(low,rg[i].a);
            sum += (rg[i].b-rg[i].a)/rg[i].c+1;
        }

        ans = Binary(low,high,n);
        if(ans == -1) puts("DC Qiang is unhappy.");
        else
        {
            for(cnt = i = 0; i < n; ++i)
            {
                if(ans >= rg[i].a && ans <= rg[i].b && (ans-rg[i].a)%rg[i].c == 0) cnt++;
            }
            printf("%I64d %I64d\n",ans,cnt);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-25 18:06:29

【HDOJ 4768】 Flyer (等差数列+二分)的相关文章

hdu 4768 Flyer(二分查找)

Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1537    Accepted Submission(s): 552 Problem Description The new semester begins! Different kinds of student societies are all trying to adv

hdu 4768 Flyer【二分】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4768 题意:学校有N个社团,新学期开始之际这N个社团发传单,它们发 传单是有规律的,有三个数组A[maxn],B[maxn],C[maxn],第i个设 团值发给编号为x的同学,其中x=A[i]+k*C[i]且k为整数,x小于等于 B[i];学校学生标号1~2^31,求那个同学收到传单数为奇数和这位同学 收到的传单数目,题目保证最多有一位同学收到传单为奇数. 分析:题目保证的那句话很重要,由于学生的数

HDU 4768 Flyer (二分)

OJ题目:click here~~ 题目分析:n个[a  b] 区间,对于i 属于[a  b]  ,从a开始,间隔c ,即i = a , i = a + c , i = a + 2*c -- 将x[ i ] 加1 ,x[ i ] 初值为0 . 已知最多只有一个x[ i ] 为奇数.找到这个i , 和这个奇数. 由于最多只有一个奇数,且奇数 + 偶数 = 奇数.用二分夹逼出这个奇数的位置.找到这个位置,再计算这个奇数就很容易了. AC_CODE const int maxn = 20002; LL

Hdoj 2333 Assemble 【二分】

Assemble Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 479    Accepted Submission(s): 169 Problem Description Recently your team noticed that the computer you use to practice for programming

HDOJ 5141 LIS again 二分

二分求LIS 对每一个位置为终点的LIS记录开头的最靠右边的值.... LIS again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 272    Accepted Submission(s): 96 Problem Description A numeric sequence of ai is ordered if a1<a2<

hdoj 1969 Pie【二分】

Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6320    Accepted Submission(s): 2383 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I h

hdu 4678

HDU 4768: Flyer 题意: 有N个社团,每个社团三个属性A,B,C,表示会向编号A+k*C的同学发传单(k=0,1,2...  && A+k*C <= B).题目保证最多有一个人收到的传单数是奇数.求如果有人收到传单是奇数,输出编号和他收到的传单数. 思路: 观察最后情况,可以发现,要么每个人都是偶数.要么有一个是奇数.观察其前缀和,发现奇数那个人之前的前缀和都是偶数,之后都是奇数.所以,二分之. 代码: (上交大牛代码--) #include <cstdio>

HDU4768:Flyer [ 二分的奇妙应用 好题 ]

传送门 Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1718    Accepted Submission(s): 622 Problem Description The new semester begins! Different kinds of student societies are all trying to

HDU4768:Flyer(二分)

Problem Description The new semester begins! Different kinds of student societies are all trying to advertise themselves, by giving flyers to the students for introducing the society. However, due to the fund shortage, the flyers of a society can onl