4 条题解

  • 0
    @ 2025-3-29 18:59:02

    #include #include #include #include #include using namespace std; typedef long long LL; const int MAXN = 1e6 + 10; const int INF = 0x3f; struct cow{ public: int l, r, id; public: void read(int i){ cin >> l >> r; id = i; } private: void debug(){ printf("l = %d,r = %d,id = %d\n", l, r, id); } }cows[MAXN]; int ans[MAXN]; int n; struct greater_q{ bool operator()(cow a, cow b){ return a.r > b.r; } }; priority_queue<cow, vector, greater_q> q; bool cmp(cow a,cow b){ if(a.l == b.l)return a.r < b.r; return a.l < b.l; } int main() { cin >> n; for(int i = 1;i <= n; i++){ cows[i].read(i); } sort(cows + 1,cows + 1 + n, cmp); int cnt = 0; for(int i = 1;i <= n; i++){ if(q.empty() || q.top().r >= cows[i].l){//开启新畜栏 cnt++; ans[cows[i].id] = cnt; q.push(cows[i]); } else{//使用旧畜栏 ans[cows[i].id] = ans[q.top().id]; q.pop(); q.push(cows[i]); } } cout << cnt << endl; for(int i = 1;i <= n; i++){ cout << ans[i] << endl; } return 0; }

    信息

    ID
    23
    时间
    1000ms
    内存
    128MiB
    难度
    2
    标签
    递交数
    166
    已通过
    100
    上传者