HDU4883 TIANKENG’s restaurant 【贪心】

TIANKENG’s restaurant

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 324    Accepted Submission(s): 167


Problem Description
TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the ith group in sum. Assuming that each customer can own only one chair. Now we know the arriving time STi and departure time EDi of each group. Could you help TIANKENG calculate the minimum chairs he needs to prepare so that every customer can take a seat when arriving the restaurant?
 

Input
The first line contains a positive integer T(T<=100), standing for T test cases in all.

Each cases has a positive integer n(1<=n<=10000), which means n groups of customer. Then following n lines, each line there is a positive integer Xi(1<=Xi<=100), referring to the sum of the number of the ith group people, and the arriving time STi and departure time Edi(the time format is hh:mm, 0<=hh<24, 0<=mm<60), Given that the arriving time must be earlier than the departure time.

Pay attention that when a group of people arrive at the restaurant as soon as a group of people leaves from the restaurant, then the arriving group can be arranged to take their seats if the seats are enough.
 

Output
For each test case, output the minimum number of chair that TIANKENG needs to prepare.
 

Sample Input
   
   
   
   
2 2 6 08:00 09:00 5 08:59 09:59 2 6 08:00 09:00 5 09:00 10:00
 

Sample Output
   
   
   
   
11 6
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 1500
using std::max_element;

int arr[maxn];

int main()
{
    int t, n, i, h, m, h2, m2, people, j;
    scanf("%d", &t);
    while(t--){
        memset(arr, 0, sizeof(arr));
        scanf("%d", &n);
        for(i = 0; i < n; ++i){
            scanf("%d %d:%d %d:%d", &people, &h, &m, &h2, &m2);
            m += h * 60; m2 += h2 * 60;
            for(j = m; j < m2; ++j) arr[j] += people;
        }
        printf("%d\n", *max_element(arr, arr + 1500));
    }
    return 0;
}

写了个线段树版本的,RE,但不知错哪了。

#include <stdio.h>
#include <string.h>
#define maxn 1502
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1

int tree[maxn << 2], lazy[maxn << 2];

int max(int a, int b)
{
    return a > b ? a : b;
}

void pushDown(int l, int r, int rt)
{
    lazy[rt << 1] += lazy[rt];
    lazy[rt << 1 | 1] += lazy[rt];
    tree[rt << 1] += lazy[rt];
    tree[rt << 1 | 1] += lazy[rt];
    lazy[rt] = 0;
}

void pushUp(int l, int r, int rt)
{
    tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]);
}

void build(int l, int r, int rt)
{
    tree[rt] = lazy[rt] = 0;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}

void update(int left, int right, int val, int l, int r, int rt)
{
    if(left == l && right == r){
        lazy[rt] += val; 
        tree[rt] += val; 
        return;
    }
    if(lazy[rt]) pushDown(l, r, rt);
    int mid = (l + r) >> 1;
    if(right <= mid) update(left, right, val, lson);
    else if(left > mid) update(left, right, val, rson);
    else{
        update(left, mid, val, lson);
        update(mid + 1, right, val, rson);
    }
    pushUp(l, r, rt);
}

int main()
{
    //freopen("stdin.txt", "r", stdin);
    int t, n, i, h0, m0, h1, m1, val;
    scanf("%d", &t);
    while(t--){
        build(0, 1500, 1);
        scanf("%d", &n);
        for(i = 0; i < n; ++i){
            scanf("%d %d:%d %d:%d", &val, &h0, &m0, &h1, &m1);
            m0 += h0 * 60; m1 += h1 * 60; --m1;
            update(m0, m1, val, 0, 1500, 1);
        }
        printf("%d\n", tree[1]);
    }
    return 0;
}


你可能感兴趣的:(HDU4883)