【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
 

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


代码如下:

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值