频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
HDU 4768 Flyer(二分)
2014-03-10 11:05:48         来源:HDU 4768 Flyer(二分)  
收藏   我要投稿

Flyer

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1445 Accepted Submission(s): 510


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 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!

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

题意:给定n个分传单,每次从a分到b间隔为c,保证最多一个人分传单为奇数,问这个人是第几个和传单数,如果不存在输出DC Qiang is unhappy.

思路:二分,计算总和,总和为偶数不可行,然后去二分奇数人的位置,计算左边一块是否是奇数,如果是就说明该人在左边往左缩,否则就往右。

代码:

#include 
#include 
#include 
const long long INF = (1LL<<31);
#include 
using namespace std;

const int N = 20005;
int n;
long long sum, a[N], b[N], c[N], l, r;

long long judge(long long mid) {
    long long sum = 0;
    for (int i = 0; i < n; i++) {
	long long br = min(mid , b[i]);
	if (br >= a[i])
	    sum += (br - a[i]) / c[i] + 1;
    }
    return sum % 2;
}

bool solve() {
    if (sum % 2 == 0) return false;
    while (l < r) {
	long long mid = (l + r) / 2;
	if (judge(mid))
	    r = mid;
	else
	    l = mid + 1;
    }
    int num = 0;
    for (int i = 0; i < n; i++) {
	if (l >= a[i] && l <= b[i]) {
	    if ((l - a[i]) % c[i] == 0)
		num++;
	}
    }
    cout << l << ' ' << num << endl;
    return true;
}

int main() {
    while (scanf("%d", &n) == 1) {
	sum = 0; l = INF; r = 0;
	for (int i = 0; i < n; i++) {
	    cin >> a[i] >> b[i] >> c[i];
	    sum += (b[i] - a[i]) / c[i] + 1;
	    l = min(l, a[i]);
	    r = max(r, b[i]);
	}
	if (!solve())
	    cout << "DC Qiang is unhappy." << endl;
    }
    return 0;
}


点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇:UVALive 6465--UVALive 6473
下一篇:hdu 4762 Cut the Cake(推导+高精度)
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站