美文网首页
HDU4768——Flyer

HDU4768——Flyer

作者: xz闲语岁月 | 来源:发表于2017-08-23 21:00 被阅读0次

    Problem

    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+2C_i,…A_i+kC_i (A_i+kC_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


    思路一

    做这道题时候的想法,因为乍一看最多一个奇数,直接暴力异或就好。

    代码一

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int N=2e4+5;
    long long a[N],b[N],c[N];
    int main(){
      ios::sync_with_stdio(false);
      cin.tie(0);
      long long n,res,sum;
      while(cin>>n){
        res=sum=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        for(long long i=1;i<=n;i++)cin>>a[i]>>b[i]>>c[i];
        for(long long i=1;i<=n;i++){
          for(long long j=a[i];j<=b[i];j+=c[i])res^=j;
        }
        if(res){
          for(long long i=1;i<=n;i++){
            if(res>=a[i]&&res<=b[i]&&(res-a[i])%c[i]==0)++sum;
          }
          cout<<res<<" "<<sum<<endl;
        }
        else cout<<"DC Qiang is unhappy."<<endl;
      }
      return 0;
    }
    

    思路二

    代码一通过没问题,不过后来经过学长提醒,这道题正确的思路是二分——所有组中最小编号为下界,最大B为上界,二分条件是所有组从A到mid(二分中点) 得到玩具的个数和为奇数时,则奇数小朋友在low--mid里,否则在mid--high里(如果有的话)。
    因为如果mid之前小朋友有一个得到奇数的话,之前所有小朋友得到玩具数一定为奇,否则为偶 (偶+偶得偶,偶+奇得奇数) 。这是因为题目说了有的话只有一个奇数,如果有多个的话奇+奇得奇,就不能这么去做了。

    代码二

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 2e4+5;
    int n;
    long long l[N],r[N],k[N];
    long long deal(long long x,long long y,long long z){
        return 1 + (y - x)/z;
    }
    int got(long long x){
        int cnt = 0;
        for(int i = 1;i <= n;i++)
         if(l[i] <= x && r[i] >= x)
          if(x % k[i] == l[i] % k[i]) cnt++;
        return cnt;
    }
    long long check(long long mid){
        long long cnt = 0;
        for(int i = 1;i <= n;i++)
         if(l[i] <= mid) cnt += deal(l[i],min(r[i],mid),k[i]);
        return cnt;
    }
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        while(cin>>n){
            long long x = 1,y = 1;
            memset(l,0,sizeof(l));
            memset(r,0,sizeof(r));
            memset(k,0,sizeof(k));
            for(int i = 1;i <= n;i++){
                cin>>l[i]>>r[i]>>k[i];
                y = max(y, r[i]);
            }
            while(x != y){
                long long mid = (x+y)>>1;
                if(check(mid) & 1) y = mid;
                else x = mid+1;
            }
            if(check(x) & 1) cout<<x<<" "<<got(x)<<endl;
            else cout<<"DC Qiang is unhappy."<<endl;
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:HDU4768——Flyer

          本文链接:https://www.haomeiwen.com/subject/xrmjdxtx.html