0


AtCoder Beginner Contest 354 C - AtCoder Magics

Problem Statement

Takahashi has 𝑁 cards from the card game "AtCoder Magics." The 𝑖-th card will be called card 𝑖. Each card has two parameters: strength and cost. Card 𝑖 has a strength of 𝐴𝑖 and a cost of 𝐶𝑖.

He does not like weak cards, so he will discard them. Specifically, he will repeat the following operation until it can no longer be performed:

  • Choose two cards 𝑥 and 𝑦 such that 𝐴𝑥>𝐴𝑦and 𝐶𝑥<𝐶𝑦​. Discard card 𝑦.

It can be proved that the set of remaining cards when the operations can no longer be performed is uniquely determined. Find this set of cards.

Constraints

  • 2≤𝑁≤2×105
  • 1≤𝐴𝑖,𝐶𝑖≤109
  • 𝐴1,𝐴2,…,𝐴𝑁​ are all distinct.
  • 𝐶1,𝐶2,…,𝐶𝑁 are all distinct.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

  1. 𝑁
  2. 𝐴1 𝐶1
  3. 𝐴2 𝐶2
  4. ⋮⋮
  5. 𝐴𝑁​ 𝐶𝑁

Output

Let there be 𝑚 remaining cards, cards 𝑖1,𝑖2,…,𝑖𝑚​, in ascending order. Print these in the following format:

  1. 𝑚
  2. 𝑖1 𝑖2 ⋯⋯ 𝑖𝑚

Sample Input 1Copy

Copy

  1. 3
  2. 2 4
  3. 1 1
  4. 3 2

Sample Output 1Copy

Copy

  1. 2
  2. 2 3

Focusing on cards 1 and 3, we have 𝐴1<𝐴3​ and 𝐶1>𝐶3​, so card 1 can be discarded.

No further operations can be performed. At this point, cards 2 and 3 remain, so print them.

Sample Input 2Copy

Copy

  1. 5
  2. 1 1
  3. 10 2
  4. 100 3
  5. 1000 4
  6. 10000 5

Sample Output 2Copy

Copy

  1. 5
  2. 1 2 3 4 5

In this case, no cards can be discarded.

Sample Input 3Copy

Copy

  1. 6
  2. 32 101
  3. 65 78
  4. 2 29
  5. 46 55
  6. 103 130
  7. 52 40

Sample Output 3Copy

Copy

  1. 4
  2. 2 3 5 6
  3. 首先声明本篇博客代码取自https://atcoder.jp/contests/abc354/editorial/10048
  4. 自己加一些感悟,用朴素的语言来解释这道题目

题目大意:

首先,要明白这道题要我们做什么,就是丢弃这些卡片,然后输出剩余卡片的编号,那么什么样的卡片要舍弃呢?如果这个卡片它又贵还又弱,那么我们就不要它。
因此我们对所有的卡片进行按照cost从小到大排序,v表示前i-1张卡片中最强的力量,如果第i张卡片力量大于等于v,说明这种卡片可以保留。反之,如果第i张卡片比i弱,又因为它还贵,那么我们就不要这张卡片了

代码实现:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<string>
  5. #include<queue>
  6. #include<vector>
  7. using namespace std;
  8. struct Card
  9. {
  10. int a;
  11. int c;
  12. int index;
  13. };
  14. int main()
  15. {
  16. int n;
  17. cin>>n;
  18. vector<Card> cards(n);
  19. for(int i=0; i<n; i++)
  20. {
  21. int a,c;
  22. cin>>a>>c;
  23. cards[i]= {a,c,i};
  24. }
  25. sort(cards.begin(),cards.end(),[&](const auto &l,const auto &r)
  26. {
  27. return l.c<r.c;
  28. });
  29. vector<int> ans;
  30. int v=0;
  31. for(int i=0;i<n;i++)
  32. {
  33. if(cards[i].a>=v)
  34. {
  35. v=cards[i].a;
  36. ans.push_back(cards[i].index);
  37. }
  38. }
  39. sort(ans.begin(),ans.end());
  40. const int m=(int)ans.size();
  41. cout<<m<<endl;
  42. for(int i=0;i<m;i++)
  43. {
  44. cout<<ans[i]+1;
  45. if(i==m-1) cout<<endl;
  46. else cout<<" ";
  47. }
  48. return 0;
  49. }
标签: c语言 开发语言

本文转载自: https://blog.csdn.net/2302_79545206/article/details/139040427
版权归原作者 王守乐 所有, 如有侵权,请联系我们删除。

“AtCoder Beginner Contest 354 C - AtCoder Magics”的评论:

还没有评论