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​ 𝐶1​
𝐴2​ 𝐶2
⋮⋮
𝐴𝑁​ 𝐶𝑁

Output

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

复制代码
𝑚
𝑖1 𝑖2​ ⋯⋯ 𝑖𝑚

Sample Input 1Copy

Copy

复制代码
3
2 4
1 1
3 2

Sample Output 1Copy

Copy

复制代码
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

复制代码
5
1 1
10 2
100 3
1000 4
10000 5

Sample Output 2Copy

Copy

复制代码
5
1 2 3 4 5

In this case, no cards can be discarded.

Sample Input 3Copy

Copy

复制代码
6
32 101
65 78
2 29
46 55
103 130
52 40

Sample Output 3Copy

Copy

复制代码
4
2 3 5 6

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

题目大意:

首先,要明白这道题要我们做什么,就是丢弃这些卡片,然后输出剩余卡片的编号,那么什么样的卡片要舍弃呢?如果这个卡片它又贵还又弱,那么我们就不要它。

因此我们对所有的卡片进行按照cost从小到大排序,v表示前i-1张卡片中最强的力量,如果第i张卡片力量大于等于v,说明这种卡片可以保留。反之,如果第i张卡片比i弱,又因为它还贵,那么我们就不要这张卡片了

代码实现:

cpp 复制代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>

using namespace std;

struct Card
{
    int a;
    int c;
    int index;
};

int main()
{
    int n;
    cin>>n;
    vector<Card> cards(n);
    for(int i=0; i<n; i++)
    {
        int a,c;
        cin>>a>>c;
        cards[i]= {a,c,i};
    }
    sort(cards.begin(),cards.end(),[&](const auto &l,const auto &r)
    {
        return l.c<r.c;
    });
    vector<int> ans;
    int v=0;
    for(int i=0;i<n;i++)
    {
        if(cards[i].a>=v)
        {
            v=cards[i].a;
            ans.push_back(cards[i].index);
        }
    }
    sort(ans.begin(),ans.end());
    const int m=(int)ans.size();
    cout<<m<<endl;
    for(int i=0;i<m;i++)
    {
        cout<<ans[i]+1;
        if(i==m-1) cout<<endl;
        else cout<<" ";
    }
    return 0;
}
相关推荐
Bear on Toilet11 分钟前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
程序员东岸25 分钟前
C语言入门指南:字符函数和字符串函数
c语言·笔记·学习·程序人生·算法
码猿宝宝33 分钟前
浏览器中javascript时间线,从加载到执行
开发语言·javascript·ecmascript
OEC小胖胖35 分钟前
App Router vs. Pages Router:我应该如何选择?
开发语言·前端·前端框架·web·next.js
max5006001 小时前
OpenSTL PredRNNv2 模型复现与自定义数据集训练
开发语言·人工智能·python·深度学习·算法
Pocker_Spades_A1 小时前
Python快速入门专业版(二十八):函数参数进阶:默认参数与可变参数(*args/**kwargs)
开发语言·python
努力努力再努力wz1 小时前
【c++进阶系列】:map和set的模拟实现(附模拟实现的源码)
java·linux·运维·开发语言·c++
Cloud Traveler2 小时前
8.FC平台模块梳理
java·linux·开发语言
歪歪1002 小时前
webpack 配置文件中 mode 有哪些模式?
开发语言·前端·javascript·webpack·前端框架·node.js
0_0梅伊阁诗人2 小时前
Flask
开发语言·数据库·python·flask