J - Flyer------(2015 summer training #11)

J - Flyer
时限:1000MS     内存:32768KB     64位IO格式:%I64d & %I64u

问题描述

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! 
 

输入

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. 
 

输出

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. 
 

样例输入

     
     
2 1 10 1 2 10 1 4 5 20 7 6 14 3 5 9 1 7 21 12
 

样例输出
   
   
1 1 8 1

分析:方法真巧,在船长的悉心讲解下,我才弄明白咋做。

题意:n个社团派发传单,有a,b,c三个参数,派发的规则是,派发给序号为a,a+c....a+k*c,序号要求是小于等于b.这其中,有一个学生只收到了奇数传单,要求找出这个学生的编号与得到的传单数目

思路:如果使用异或运算,也还是比较简单的,但是这样的话所花费的时间就比较长,正确的做法是使用二分使用二分来划分区间,由于是每个社团得到的序列都是等差数列,所以我们很容易能得到区间派发的传单数。如果是奇数,那么所求的人肯定在左区间,否则在右区间,这样二分下去找到答案。

CODE:
#include <iostream>
#include <limits.h>
#include <cstdio>
using namespace std;

int main()
{
    const int maxn=20005;
    int N,A[maxn],B[maxn],C[maxn];
    while(cin>>N){
        for(int i=0;i<N;i++)
            scanf("%d%d%d",&A[i],&B[i],&C[i]);
        long long l=1,r=INT_MAX,mid;
        while(l<r){
            mid=(l+r)/2;
            int sum=0;
            for(int i=0;i<N;i++){
                if(mid<B[i]&&mid>=A[i])
                    sum+=(mid-A[i])/C[i]+1;
                else if(mid>=B[i])
                    sum+=(B[i]-A[i])/C[i]+1;
            }
            if(sum&1)
                r=mid;
            else
                l=mid+1;
        }
        long long ans=0;
        for(int i=0;i<N;i++){
            if(r>=A[i]&&r<=B[i]&&(r-A[i])%C[i]==0)
                ans++;
        }
        if(ans&1)
            printf("%lld %lld\n",r,ans);
        else
            printf("DC Qiang is unhappy.\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值