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

题意:n个社团派发传单,有a,b,c三个参数,派发的规则是,派发给序号为a,a+c....a+k*c,序号要求是小于等于b

这其中,有一个学生只收到了奇数传单,要求找出这个学生的编号与得到的传单数目

思路:如果使用异或运算,也还是比较简单的,但是这样的话所花费的时间就比较长,正确的做法是使用二分

使用二分来划分区间,由于是每个社团得到的序列都是等差数列,所以我们很容易能得到区间派发的传单数

如果是奇数,那么所求的人肯定在左区间,否则在右区间,这样二分下去找到答案

二分代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ll __int64
#define L 20005

ll a[L],b[L],c[L],l,r,n;

ll solve(ll mid)
{
    ll k,sum = 0;
    int i;
    for(i = 0; i<n; i++)
    {
        k = min(mid,b[i]);
        if(k>=a[i])
            sum+=(k-a[i])/c[i]+1;
    }
    return sum;
}

int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        for(i = 0; i<n; i++)
            scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]);
        l = 0,r = 1LL<<31;
        ll mid;
        while(l<r)
        {
            mid = (l+r)/2;
            if(solve(mid)%2) r = mid;
            else l = mid+1;
        }
        if(l == 1LL<<31)
            printf("DC Qiang is unhappy.\n");
        else
        {
            while(solve(l)%2==0)l--;
            printf("%I64d %I64d\n",l,solve(l)-solve(l-1));
        }
    }

    return 0;
}

异或运算

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ll __int64
#define L 20005

ll a[L],b[L],c[L];

int main()
{
    int i,j,n;
    while(~scanf("%d",&n))
    {
        for(i = 0; i<n; i++)
            scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]);
        ll ans = 0,cnt = 0;
        for(i = 0; i<n; i++)
            for(j = a[i]; j<=b[i]; j+=c[i])
                ans^=j;
        if(ans)
        {
            for(i = 0; i<n; i++)
                for(j = a[i]; j<=b[i]; j+=c[i])
                    if(ans == j) cnt++;
        printf("%I64d %I64d\n",ans,cnt);
        }
        else
        printf("DC Qiang is unhappy.\n");
    }

    return 0;
}

HDU4768:Flyer(二分)

时间: 2024-08-13 23:21:52

HDU4768:Flyer(二分)的相关文章

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

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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1632    Accepted Submission(s): 591 Problem Description The new semester begins! Different kinds of student societies are all trying to adv

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

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,求那个同学收到传单数为奇数和这位同学 收到的传单数目,题目保证最多有一位同学收到传单为奇数. 分析:题目保证的那句话很重要,由于学生的数

[hdu4768]二分

http://acm.hdu.edu.cn/showproblem.php?pid=4768 题意:n个传单分别发给编号为ai, ai + ci, ai + 2 * ci, .. , ai + k * ci的学生,其中k 是满足 ai + k * ci <= bi 的最大的k,题目保证收到传单的个数为奇数的学生最多只有一个,求这个学生的编号和收到的传单数. 方法:考虑前i个学生,sum[i]表示前i个学生收到传单的个数的奇偶性,如果sum[i]为奇数,证明那个学生一定在前i个,否则在i之后,据此

hdu4768二分答案

/* 如果发的传单是偶数,那么所有人都收到双数张. 仅考虑发了单数张传单,二分答案x,如果x左边是偶数,那么答案在右侧,如果x左边是奇数,那么答案在左侧 */ #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define ll long long #define maxn 20005 #define INF 1000000009 using namespac

POJ4768(二分区间)

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