UVALive 5864 - Register Allocation

第一种办法,模拟:每次尝试把a[i]插入到reg中,如果找不到可以插入的reg,cnt++,一次插入一个a[i]

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<algorithm>
using namespace std;
typedef pair<int, int>Pi;
const int maxn = 1e4+5;
Pi a[maxn];
int reg[maxn];

bool cmp(const Pi &a, const Pi &b){
    return a.first < b.first;
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n;
        memset(reg, 0, sizeof(reg));
        scanf("%d", &n);
        for(int i = 0; i < n; i++)scanf("%d%d", &a[i].first, &a[i].second);
        sort(a, a+n, cmp);
        int cnt = 1;
        for(int i = 0; i < n; i++){
            bool isFind = false;
            for(int j = 0; j < cnt; j++){
                if(a[i].first > reg[j]){
                    isFind = true;
                    reg[j] = a[i].second;
                    break;
                }
            }
            if(!isFind){
                cnt++;
                reg[cnt-1] = a[i].second;
            }
        }

        printf("%d\n", cnt);
    }
    return 0;
}

第二种办法,就看一个时间点最多被重叠几次,就需要几个reg

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 3e4+5;
int a[maxn];

bool cmp (const int & a, const int & b){
    return a>b;
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        memset(a, 0, sizeof(a));
        int n;
        scanf("%d", &n);
        while(n--){
            int s, t;
            scanf("%d%d", &s, &t);
            for(int i = s; i <= t; i++)a[i]++;
        }
        sort(a, a+maxn, cmp);
        printf("%d\n", a[0]);

    }
    return 0;
}


发布了14 篇原创文章 · 获赞 3 · 访问量 1139

猜你喜欢

转载自blog.csdn.net/qq_33808530/article/details/51935032
今日推荐