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 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   |   We have carefully selected several similar problems for you:  5173 5170 5169 5168 5165

题意及题解转自:http://blog.csdn.net/libin56842/article/details/25741317

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

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

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

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

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

12940329 2015-02-12 17:35:01 Accepted 4768 78MS 1756K 1933 B G++ czy
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<stack>
 11 #include<string>
 12
 13 #define N 20005
 14 #define M 205
 15 #define mod 10000007
 16 //#define p 10000007
 17 #define mod2 1000000000
 18 #define ll long long
 19 #define LL long long
 20 #define eps 1e-6
 21 #define inf 100000000
 22 #define maxi(a,b) (a)>(b)? (a) : (b)
 23 #define mini(a,b) (a)<(b)? (a) : (b)
 24
 25 using namespace std;
 26
 27 int n;
 28 ll a[N],b[N],c[N];
 29 ll num[N];
 30 ll ans;
 31 ll ma;
 32 ll cc;
 33
 34 void ini()
 35 {
 36     int i;
 37     ma=0;
 38     for(i=1;i<=n;i++){
 39         scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]);
 40         ma=max(ma,b[i]);
 41         num[i]=(b[i]-a[i])/c[i]+1;
 42     }
 43 }
 44
 45 ll ok(ll l,ll r)
 46 {
 47     int i;
 48     ll re;
 49     re=0;
 50     ll t1,t2;
 51     l--;
 52     for(i=1;i<=n;i++){
 53         if(l>b[i] || r<a[i]) continue;
 54         if(l<a[i]){
 55             t1=0;
 56         }
 57         else{
 58             t1=(l-a[i])/c[i]+1;
 59         }
 60         if(r>b[i]){
 61             t2=num[i];
 62         }
 63         else{
 64             t2=(r-a[i])/c[i]+1;
 65         }
 66         re+=t2-t1;
 67     }
 68     if(re%2==1) return re;
 69     else return 0;
 70 }
 71
 72 void solve()
 73 {
 74     ll l,r,mid;
 75     l=0;r=ma;
 76     while(l<r)
 77     {
 78         mid=(l+r)/2;
 79         cc=ok(l,mid);
 80         if(cc!=0)
 81         {
 82             r=mid;
 83         }
 84         else{
 85             l=mid+1;
 86         }
 87     }
 88     ans=l;
 89     cc=ok(l,l);
 90 }
 91
 92 void out()
 93 {
 94     if(cc==0){
 95         printf("DC Qiang is unhappy.\n");
 96     }
 97     else
 98         printf("%I64d %I64d\n",ans,cc);
 99 }
100
101 int main()
102 {
103     //freopen("data.in","r",stdin);
104     //freopen("data.out","w",stdout);
105     //scanf("%d",&T);
106     //for(int ccnt=1;ccnt<=T;ccnt++)
107     //while(T--)
108     //scanf("%d%d",&n,&m);
109     while(scanf("%d",&n)!=EOF)
110     {
111         if(n==0) break;
112         ini();
113         solve();
114         out();
115     }
116     return 0;
117 }
时间: 2024-08-05 19:34:30

HDU4768:Flyer [ 二分的奇妙应用 好题 ]的相关文章

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

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

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

二分查找的相关算法题

最近笔试经常遇到二分查找的相关算法题 1)旋转数组中的最小数字 2)在旋转数组中查找某个数 2)排序数组中某个数的出现次数 下面我来一一总结 1 旋转数组的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1. 实现数组的旋转见左旋转字符串. 和二分查找法一样,用两个指针分别指向数组的第一个元素和最后一个元素. 我们注意到旋转

POJ 3104 Drying [二分 有坑点 好题]

传送门 表示又是神题一道 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9327   Accepted: 2364 Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring proces

Codeforces Round #404 (Div. 2) C 二分,水 D 数学,好题 E 分块

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 题意:仓库容量n,每天运来m粮食,第 i 天被吃 i 粮食,问第几天仓库第一次空掉. tags:==SB题 注:二分边界判断,数据范围爆long long判断. // CF404 C #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000&

HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Problem Description The h-index of an author is the largest h where he has at least h papers with citations not les

POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以行列分别作为2个顶点集 首先以每一行来看,把这一行里面连续的*编号,作为一个顶点 再以每一列来看,把这一列里面连续的*编号,作为一个顶点 则每一个*都有2个编号,以行看时有一个,以列看时有一个,则把这2个编号连边,容量为1 再建一个源点,连接所有行的编号,一个汇点,连接所有列的编号 这道题要求的是,

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