HDU 4768 (二分区间)

题意:告诉n组A,B,C,按照A + k * C生成等差数列,问这n组数列中哪个数字出现了奇数次以及出现了几次,题目保证最多只会出现一个这种数字。

分析:读完题并没有思路,后来知道是二分区间,枚举是哪个数字出现了奇数次,算该数字之前一共有几个数字,如果是奇数个,说明答案就在[L , Mid]中。

PS:之前用二分只是枚举具体要求的数字,原来枚举区间也可以。二分的时候 l = mid + 1,r = mid ;(一开始r = mid + 1,l = mid就无限循环,手算并没有错啊?很奇怪。。。)

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(int i = a; i < b; i++)
18 #define repd(i, a, b) for(ll i = b; i >= a; i--)
19 #define sfi(n) scanf("%d", &n)
20 #define pfi(n) prllf("%d\n", n)
21 #define INF 0x3f3f3f3f3f
22 #define N 20010
23 ll a[N],b[N],c[N];
24 int n;
25 ll judge(ll p)///p之前已经发了多少张传单了
26 {
27     ll sum = 0;
28     repu(i,0,n)
29     {
30         ll t = min(p,b[i]);
31         if(t >= a[i])
32             sum += ((t - a[i])/c[i] + 1ll);
33     }
34     return sum%2ll;
35 }
36 int main()
37 {
38     while(~scanf("%d",&n))
39     {
40         ll sum = 0ll,t = 0ll,l = INF,r = 0ll;
41         repu(i,0,n)
42         {
43             scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]);
44             r = max(r,b[i]);
45             l = min(l,a[i]);
46         }
47         if(!judge(r))
48         {
49             printf("DC Qiang is unhappy.\n");
50             continue;
51         }
52         ll mid = 0ll;
53         while(l < r)
54         {
55             mid = (l + r)/2ll;
56             if(judge(mid))///如果是奇数
57                 r = mid ;
58             else
59                 l = mid +1ll;
60         }
61         ///求具体有几张
62         sum = 0ll;
63         repu(i,0,n)
64         {
65             if(r >= a[i] && r <= b[i])
66                 if((r - a[i])%c[i] == 0)
67                     sum++;
68         }
69         printf("%I64d %I64d\n",r,sum);
70     }
71     return 0;
72 }

时间: 2024-10-13 12:23:31

HDU 4768 (二分区间)的相关文章

hdu 4768 二分

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

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

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 5875 st+二分区间

题目大意:给你n个数,q次询问,每次询问区间[l, r],问a[i]%a[i + 1] % a[i + 2]...%a[j](j <= r)的值 思路:st预处理维护,在二分区间,复杂度n*(logn)*logn //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; #define LL long long #define ALL(a) a.begin(), a.en

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

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 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in