POJ 3110 Jenny's First Exam (二分 + 树状数组 + 贪心 + 预处理年份)
来源:互联网 发布:cs1.5和1.6知乎 编辑:程序博客网 时间:2024/06/06 08:02
大体题意:
告诉你n 个科目的考试日期,在考试当天不能复习,每一个科目的最早复习时间不能早于考试时间的t天,每一天你可以复习完一科,也只能复习一科,求最晚的复习时间!
思路:
题目中描述的考试时间是1900年到2100年 总共200年,但这只是考试日期,还有早于考试时间的t天,t最多是10w,2年多!因此我们要计算1600年到2100年这 500年的时间!
为了方便计算,我们先把日期映射成数字,方法可以预处理 从1600年1月1日 一直加 一直加 直到年份不超过2100年!
这样预处理后 我们能够把数字映射成年份,在手写个二分 把年份映射成 数字!
然后肯定是某种排序后 进行扫描每一个科目 定一个时间!
这里排序采用贪心的策略 优先给那些 最晚给科目的左边界(即最早开始复习这一科的时间) 处理!
因为这样贪心 不会让一科占用不该占用的位置。可以画几个线段看一下!
然后我们在给这一个科目的区间选择一个点 最晚 且必须复习这一科。 枚举肯定会超时 毕竟最多有10w天,我们采用二分 + 树状数组的方式,树状数组 统计 1~i 结点有多少天被占用了! 二分中间的点,当中点到区间右端点全都被占用了 并且 中点没有被占用 这就是一个最合理的点! 否则 继续缩小区间来分!
注意:预处理年份 尽量写的好一些 ,写的不好 容易超时!!!
#include <cstdio>#include <cstring>#include <algorithm>#define Min(a,b) (a) > (b) ? (b) : (a)#define Max(a,b) (a) > (b) ? (a) : (b)using namespace std;int n;char s[100];int c[1000000 + 7];const int inf = 0x3f3f3f3f;int mon[] = {23333,31,28,31,30,31,30,31,31,30,31,30,31};bool is_leap(int& y){ if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) return true; return false;}bool la(int& m){ return m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12;}struct data{ int y,m,d; void read(){ scanf("%d.%d.%d",&d, &m, &y); } void init(){ y = 1600; m = d = 1; } void add(){ d++; if (m != 2){ if (d > mon[m]) { d = 1; ++m; if (m > 12)m = 1,y++; } } else{ if (d > is_leap(y) + 28) { m = 3; d = 1; } } } bool operator == (const data& rhs ) const { return y == rhs.y && m == rhs.m && d == rhs.d; } bool operator < (const data& rhs) const { return y < rhs.y || (y == rhs.y && m < rhs.m ) || ((y == rhs.y && m == rhs.m && d < rhs.d)); } void print(){ printf("%02d.%02d.%d\n",d,m,y); }}d[1000000 + 7];int cnt;void init(){ data tmp; tmp.init(); cnt = 0; while(tmp.y < 2101){ d[cnt++] = tmp; tmp.add(); }}int Map(data& rhs){ int l = 0,r = cnt; while(l <= r){ int mid = l + r >> 1; if (d[mid] == rhs) return mid; else if (rhs < d[mid])r = mid - 1; else l = mid + 1; } return -1;}struct Course{ int End,t; bool operator < (const Course & rhs) const { return End-t < rhs.End - rhs.t; }}p[50007];int lowbit(int x){ return x & -x;}int sum(int x){ int ans = 0; while(x){ ans += c[x]; x -= lowbit(x); } return ans;}void add(int pos){ while (pos < cnt){ c[pos]++; pos += lowbit(pos); }}int solve(){ int ans = inf,res; for (int i = n - 1; ~i; --i){ int l = p[i].End - p[i].t, r = p[i].End; bool ok = 0; res = -1; while(l <= r) { int mid = l + r >> 1; if (sum(r) - sum(mid) == r - mid){ if (sum(mid) - sum(mid-1) == 0){ res = Max(res,mid); add(mid); ok = 1; break; } else r = mid - 1; } else l = mid + 1; } if (!ok) return -1; ans = Min(ans,res); } return ans;}int main(){ init(); data t1; while (~scanf("%d",&n)){ memset( c,0,sizeof c); for (int i = 0; i < n; ++i){ scanf("%s",s); t1.read(); p[i].End = Map(t1); add(p[i].End); scanf("%d",&p[i].t); } sort(p,p+n); int ans = solve(); if (~ans)d[ans].print(); else puts("Impossible"); } return 0;}
Description
First exams cause many problems to Jenny. One problem is that Jenny needs the whole day to prepare for any exam (good news is she needs only one day for any preparation). Another problem: in a day of the exam Jenny is not able to study anything. And the main problem: Jenny must prepare for i-th exam not earlier then ti days before it, in the other case she forgets absolutely everything by the time of the exam.
Jenny wants to start preparations as later as possible but she has to pass all exams. Help Jenny to choose a day when she must start.
Input
The first line of the input file contains n (1 ≤ n ≤ 50 000) — the number of exams. The following lines describes exams.
Each description consists of three lines. The first line is the name of the subject (a string containing only Latin letters, maximal length is 10). The second line is the date of the exam in formatdd.mm.yyyy
. The third line contains ti for this exam (1 ≤ ti ≤ 100 000).
All exams take place in interval from 01.01.1900 to 31.12.2100.
Recall that if the year is divisible by 4 and is not divisible by 100, or is divisible by 400 — it is the leap one. Such year has 366 days, the additional day is on February 29.
Output
Output the latest date when Jenny may start preparation and pass all exams. Write date in format dd.mm.yyyy
. If it is impossible to pass all the exams, output the word “Impossible
”.
Sample Input
sample input #13Philosophy01.01.19001Algebra02.01.19003Physics04.01.190010sample input #22Philosophy29.06.20051Algebra30.06.20052
Sample Output
sample output #130.12.1899sample output #2Impossible
Source
- POJ 3110 Jenny's First Exam (二分 + 树状数组 + 贪心 + 预处理年份)
- 贪心+数据结构-poj 3110 Jenny's First Exam
- POJ-2892(树状数组 + 二分)
- POJ 2892 树状数组+二分
- poj 2892 树状数组 二分
- poj 2182 树状数组+二分
- poj 2886 二分+树状数组
- hdu5338 贪心+线段树+二分+树状数组
- hdu 2852 KiKi's K-Number(树状数组+二分)
- KiKi's K-Number(树状数组+二分)
- HDU 2852 KiKi's K-Number(树状数组+二分)
- HDU KiKi's K-Number (树状数组+二分查找)
- HDU 5592 ZYB's Premutation(二分+树状数组)
- HDU2852 KiKi's K-Number(二分+树状数组)
- HDU 2852 KiKi's K-Number(树状数组+二分)
- hdoj 2852 KiKi's K-Number(树状数组+二分)
- HDU 2852 KiKi's K-Number (树状数组+二分)
- POJ 题目2182 Lost Cows(树状数组+二分)
- Linux下的C语言编程——文件存储链表实现的通讯录
- 约瑟夫问题
- 2016.11.15
- 创建元素
- 使用UUID的优缺点和一个页面能否使用多个表单和表单之间是否可以嵌套
- POJ 3110 Jenny's First Exam (二分 + 树状数组 + 贪心 + 预处理年份)
- Java8新特性——Optional
- C 字符数组与字符串的使用及加结束符'\0'的问题
- 制作根文件系统(一)
- JQuery 实现菜单
- fgets与gets的区别
- flock()函数--文件锁
- java.lang.NullPointerException
- Talk is cheap, show me the code.