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,求那个同学收到传单数为奇数和这位同学

收到的传单数目,题目保证最多有一位同学收到传单为奇数。

分析:题目保证的那句话很重要,由于学生的数目很大,所以不能考虑

枚举、暴力,联想到奇偶的传递性,在这道题中:偶+偶=偶,奇+偶=奇,

这样可以用二分来解决,mid=(l+r), sum=cal(mid)计算出1-mid

所有学生收到的传单总和,若sum为偶数则说明1-mid中不存在那位同学

否则存在,这样时间这个问题就迎刃而解了,做题真的需要思维联系,能够

充分利用所学知识,不然就白瞎了。

献上代码:

#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<iostream>

using namespace std;

const int maxh=20000+10;

const long long int inf=(1LL<<31);

long long int A[maxh],B[maxh],C[maxh];

long long int cal(long long int mid,int n)

{

long long int top,sum=0;

for(int i=0;i<n;i++)

{

top=min(mid,B[i]);

if(top>=A[i])

sum+=(top-A[i])/C[i]+1;

}

return sum;

}

int main()

{

int n;

long long int l,r,mid;

while(~scanf("%d",&n))

{

for(int i=0;i<n;i++)

scanf("%I64d%I64d%I64d",&A[i],&B[i],&C[i]);

l=0,r=inf;

while(l<r)

{

mid=(l+r)/2;

if(cal(mid,n)%2)r=mid;

else l=mid+1;

}

if(l==inf)

printf("DC Qiang is unhappy.\n");

else

printf("%I64d %I64d\n",l,cal(l,n)-cal(l-1,n));

}

return 0;

}

时间: 2024-08-10 23:15:54

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

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 (二分区间)

题意:告诉n组A,B,C,按照A + k * C生成等差数列,问这n组数列中哪个数字出现了奇数次以及出现了几次,题目保证最多只会出现一个这种数字. 分析:读完题并没有思路,后来知道是二分区间,枚举是哪个数字出现了奇数次,算该数字之前一共有几个数字,如果是奇数个,说明答案就在[L , Mid]中. PS:之前用二分只是枚举具体要求的数字,原来枚举区间也可以.二分的时候 l = mid + 1,r = mid ;(一开始r = mid + 1,l = mid就无限循环,手算并没有错啊?很奇怪...)

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

hdu 3641 数论 二分求符合条件的最小值数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*==================================================== 二分查找符合条件的最小值 ======================================================*/ ll solve() { __int64 low = 0, high = INF, mid ; while(low <=

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

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 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

HDU 2295 DLX 二分

Radar Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2229    Accepted Submission(s): 888 Problem Description N cities of the Java Kingdom need to be covered by radars for being in a state of w