• 2015 Multi-University Training Contest 6 hdu 5360 Hiking


    Hiking

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 226    Accepted Submission(s): 126
    Special Judge


    Problem Description
    There are n soda conveniently labeled by 1,2,,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hiking except him is no less than li and no larger than ri. beta will follow the rules below to invite soda one by one:
    1. he selects a soda not invited before;
    2. he tells soda the number of soda who agree to go hiking by now;
    3. soda will agree or disagree according to the number he hears.

    Note: beta will always tell the truth and soda will agree if and only if the number he hears is no less than li and no larger than ri, otherwise he will disagree. Once soda agrees to go hiking he will not regret even if the final total number fails to meet some soda's will.

    Help beta design an invitation order that the number of soda who agree to go hiking is maximum.
     
    Input
    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

    The first contains an integer n (1n105), the number of soda. The second line constains n integers l1,l2,,ln. The third line constains n integers r1,r2,,rn(0lirin)
    It is guaranteed that the total number of soda in the input doesn't exceed 1000000. The number of test cases in the input doesn't exceed 600.
     
    Output
    For each test case, output the maximum number of soda. Then in the second line output a permutation of 1,2,,n denoting the invitation order. If there are multiple solutions, print any of them.
     
    Sample Input
    4
    8
    4 1 3 2 2 1 0 3
    5 3 6 4 2 1 7 6
    8
    3 3 2 0 5 0 3 6
    4 5 2 7 7 6 7 6
    8
    2 2 3 3 3 0 0 2
    7 4 3 6 3 2 2 5
    8
    5 6 5 3 3 1 2 4
    6 7 7 6 5 4 3 5
     
    Sample Output
    7
    1 7 6 5 2 4 3 8
    8
    4 6 3 1 2 5 8 7
    7
    3 6 7 1 5 2 8 4
    0
    1 2 3 4 5 6 7 8
     
    Source
     
    解题:贪心,先按L排序,再按y排序,把L不大于agree的全部入优先队列,然后进行贪心即可
     
     1 #include <bits/stdc++.h>
     2 #define pii pair<int,int>
     3 using namespace std;
     4 const int maxn = 100010;
     5 struct SODA {
     6     int l,r,id;
     7     SODA(int x = 0,int y = 0,int z = 0) {
     8         l = x;
     9         r = y;
    10         id = z;
    11     }
    12     bool operator<(const SODA &t) const {
    13         if(l == t.l) return r < t.r;
    14         return l < t.l;
    15     }
    16     bool operator>(const SODA &t) const {
    17         return r > t.r;
    18     }
    19 } s[maxn];
    20 struct node {
    21     int l,r,id;
    22 };
    23 priority_queue<SODA,vector<SODA>,greater<SODA> >q;
    24 vector<int>ans;
    25 bool vis[maxn];
    26 int main() {
    27     int kase,n;
    28     scanf("%d",&kase);
    29     while(kase--) {
    30         scanf("%d",&n);
    31         for(int i = 0; i < n; ++i) {
    32             scanf("%d",&s[i].l);
    33             s[i].id = i + 1;
    34         }
    35         for(int i = 0; i < n; ++i)
    36             scanf("%d",&s[i].r);
    37         sort(s,s+n);
    38         int agree = 0,cur = 0;
    39         ans.clear();
    40         bool flag = true;
    41         memset(vis,false,sizeof vis);
    42         while(cur < n && flag) {
    43             while(cur < n && s[cur].l <= agree)
    44                 q.push(s[cur++]);
    45             flag = false;
    46             while(!q.empty()) {
    47                 int u = q.top().r;
    48                 if(u >= agree) {
    49                     ans.push_back(q.top().id);
    50                     agree++;
    51                     flag = true;
    52                     vis[q.top().id] = true;
    53                     q.pop();
    54                     break;
    55                 }
    56                 q.pop();
    57             }
    58         }
    59         while(!q.empty()) {
    60             int u = q.top().r;
    61             if(u >= agree) {
    62                 ans.push_back(q.top().id);
    63                 agree++;
    64                 vis[q.top().id] = true;
    65             }
    66             q.pop();
    67         }
    68         for(int i = 1; i <= n; ++i)
    69             if(!vis[i]) ans.push_back(i);
    70         printf("%d
    ",agree);
    71         for(int i = 0; i < n; ++i)
    72             printf("%d%c",ans[i],i+1==n?'
    ':' ');
    73     }
    74     return 0;
    75 }
    View Code
  • 相关阅读:
    TCP断开连接的相关问题
    TCP建立连接的相关问题
    TCP的基本认识
    输入网址到网页显示,发生了什么
    leetcode_32.最长有效括号
    第六章:HTTP首部
    第五章:与HTTP协作的Web服务器
    RDF、RDFS、OWL
    分布式事务
    第四章:返回结果的HTTP状态码
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4709025.html
Copyright © 2020-2023  润新知