POJ 3110 Jenny's First Exam (二分 + 树状数组 + 贪心 + 预处理年份)

49 篇文章 0 订阅
32 篇文章 0 订阅

大体题意:

告诉你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;
}

Jenny's First Exam
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 792 Accepted: 208
Case Time Limit: 2000MS

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 #1
3
Philosophy
01.01.1900
1
Algebra
02.01.1900
3
Physics
04.01.1900
10

sample input #2
2
Philosophy
29.06.2005
1
Algebra
30.06.2005
2

Sample Output

sample output #1
30.12.1899

sample output #2
Impossible

Source

Northeastern Europe 2005, Northern Subregion

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值