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 a[maxn] , b[maxn] , c[maxn] , n , aa , bb;
LL cal(LL x){//计算下标从最左边 到 x的所有数之和
    LL num = 0 ;
    for(LL i = 1;i <= n;i++)
        if(x >= b[i]) num += (b[i] - a[i])/c[i] + 1;
        else if(a[i] <= x) num += (x - a[i])/c[i] + 1;
    return num;
}

void solve(){
    LL i , j , k , mid , num;
    while(aa <= bb){
        mid = (aa + bb)>>1;
        num = cal(mid);
        if(num&1) bb = mid;//如果num为奇数,则要找的那个奇数在mid左边,继续二分,以求精确
        else aa = mid + 1;
        if(aa == bb) break;//查找区间已经缩小到一个位置,就结束二分。
    }
    num = 0;
    for(i = 1;i <= n;i++)//计算二分结束的位置的数
        if(a[i] <= aa && aa <= b[i] && (aa - a[i])%c[i] == 0) num++ ;

    if(num&1) cout << aa  << " " <<num << endl;//如果是奇数,就找到了
    else
        puts("DC Qiang is unhappy.");

}

int main(){
    LL i;
    while(scanf("%lld" , &n) != EOF){
        aa = 1 << 40 , bb = -1;
        for(i = 1;i <= n;i++){
            scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);
            if(a[i] < aa) aa = a[i];
            if(b[i] > bb) bb = b[i];
        }
        solve();
    }
}

HDU 4768 Flyer (二分),布布扣,bubuko.com

时间: 2024-10-09 14:17:21

HDU 4768 Flyer (二分)的相关文章

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

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