zoukankan      html  css  js  c++  java
  • BZOJ4010: [HNOI2015]菜肴制作(拓扑排序 贪心)

    题意

    题目链接

    Sol

    震惊,HNOI竟出NOI原题

    直接在反图上贪心一下。

    // luogu-judger-enable-o2
    // luogu-judger-enable-o2
    #include<bits/stdc++.h>
    #define Pair pair<int, int>
    #define MP(x, y) make_pair(x, y)
    #define fi first
    #define se second
    using namespace std;
    const int MAXN = 2e5 + 10;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, M, a[MAXN], inder[MAXN], tmp[MAXN], ans[MAXN];
    vector<int> v[MAXN];
    void Topsort() {
        priority_queue<Pair> q;
        for(int i = 1; i <= N; i++) 
            if(!inder[i]) q.push(MP(a[i], i));
        int tot = 0;
        while(!q.empty()) {
            int p = q.top().se; q.pop(); ans[++tot] = p;
            for(int i = 0, to; i < v[p].size(); i++) {
                to = v[p][i];
                inder[to]--;
                if(!inder[to]) q.push(MP(a[to], to));
            }
        }
        for(int i = tot; i >= 1; i--) printf("%d ", ans[i]); puts("");
    }
    int solve(int x) {
        memcpy(inder, tmp, sizeof(tmp));
        priority_queue<Pair> q;
        inder[x] = N;
        for(int i = 1; i <= N; i++) if(!inder[i]) q.push(MP(a[i], i));
        int tim = N; 
        for(int i = N; i; i--) {
        	if(q.empty() || (q.top().fi < i)) return i;
            int p = q.top().se; q.pop(); 
            for(int i = 0, to; i < v[p].size(); i++) {
                to = v[p][i];
                inder[to]--;
                if(!inder[to]) q.push(MP(a[to], to));
            }
        }
        return tim;
    }
    int main() {
        N = read(); M = read();
        for(int i = 1; i <= N; i++) a[i] = read();
        for(int i = 1; i <= M; i++) {
            int x = read(), y = read();
            v[y].push_back(x); inder[x]++; tmp[x]++;
        }
        Topsort();
        for(int i = 1; i <= N; i++) printf("%d ", solve(i));
        return 0;
    }
    /*
    10 10
    4 4 3 6 9 9 10 7 10 7
    2 9
    3 5
    6 7
    1 5
    7 9
    10 2
    3 8
    8 6
    3 10
    8 5
    
    
    */
    
  • 相关阅读:
    修复grub引导Centos8和Windows
    少儿编程到底学什么?
    Windows平台最方便最易用的法语输入法
    ServiceStack.OrmLite 入门(一)
    起步:Proteus 8 仿真 Arduino 1.8.2
    命令行方式登录PostgreSQL
    CentOS 7 安装 PostgreSQL
    Twitter开源的Heron快速安装部署教程
    centos7下使用yum安装mysql
    [转]centos7 下安装MongoDB
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9817956.html
Copyright © 2011-2022 走看看