首页 > 其他分享 >ABC354

ABC354

时间:2024-06-05 20:33:58浏览次数:16  
标签:sort int 卡牌 st ABC354 ans id

C

multiset和sort,同时维护两个数组,好题,力量最强的并且花费最小的

#include<bits/stdc++.h>

using namespace std;

const int N = 3e5+20;

struct Node
{
    int a,c,id;//a是力量,c是花费,id是编号
    bool operator <(const Node &t)const{
        return a<t.a;
    }
}a[N];

int n;
multiset<int>st;//维护所有力量大于当前卡牌的花费
vector<int>ans;//存储最强卡片的编号

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].a>>a[i].c;
        a[i].id=i;//存储编号
        st.insert(a[i].c);
    }
    
    sort(a+1,a+n+1);
    int j=1;
    for(int i=1;i<=n;i++)
    {
       while(j<=n&&a[i].a==a[j].a)
       {
           st.erase(a[j++].c);//每次遍历到一张卡牌时,用一个指针将所有力量与当前卡牌相同的卡牌的花费从set中删除掉
           //这样set中所有元素对应的力量都是比当前卡牌大的
       }
       if(st.empty()||*st.begin()>=a[i].c)
       {
           ans.push_back(a[i].id);//把留下来的卡牌的编号存到ans中
       }
    }
    sort(ans.begin(),ans.end());//按照升序输出
    cout<<ans.size()<<endl;
    for(auto man:ans)cout<<man<<" ";
    
    return 0;
}
//38行那么只要存在其中一张的花费比当前卡牌小,当前卡牌就会被删除,
//即,只有当前卡牌的花费比后面所有牌的花费均小(小于等于),当前卡牌就不会被删除,由于multiset也会排序,
//那么只需要与multiset存储的第一个元素比较即可,如果当前卡牌的花费小于等于存储的最小的花费,那么当前卡牌就会被保留。
//也就是说后面的卡牌的力量比你大,但是你的花费比后面的卡牌小,那么你就会留下来

标签:sort,int,卡牌,st,ABC354,ans,id
From: https://blog.csdn.net/2301_79203206/article/details/139481314

相关文章

  • ABC354
    Alink模拟整个过程即可。点击查看代码#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;signedmain(){ inth; cin>>h; intday=1ll,g=0ll; while(g<h){ g+=(1ll<<day); day++; } cout<<day; return......
  • ABC354 E - Remove Pairs 做题笔记
    ABC354E-RemovePairs做题笔记题目链接对于这种带有博弈论的dp,考虑这样设计状态:令\(f_s\in\{1,0\}\)表示“游戏局面”为\(s\)时,先手必胜还是必败。本题中,“游戏局面”可以表示为剩余卡牌的编号集合。又因为本题中\(N\)​很小,通过状压,可以直接用一个int表示游戏......
  • AtCoder abc354E
    原题链接ProblemStatementTakahashiandAokiareplayingagameusing\(N\)cards.Thefrontsideofthe\(i\)-thcardhas\(A_i\)writtenonit,andthebacksidehas\(B_i\)writtenonit.Initially,the\(N\)cardsarelaidoutonthetable.Wit......
  • 「杂题乱刷」AT_abc354_f
    大家一起来做下这个典题。链接(at)链接(luogu)我们很容易可以想到处理前后缀的最长上升子序列的长度,然后容易\(O(n\log_2n)\)预处理。做完了。点击查看代码/*Tips:你数组开小了吗?你MLE了吗?你觉得是贪心,是不是该想想dp?一个小时没调出来,是不是该考虑换题?打cf不要......
  • [ABC354D]
    https://www.luogu.com.cn/problem/AT_abc354_dhttps://atcoder.jp/contests/abc354/tasks/abc354_d由图片可知,很显然每个\(4\times2\)​网格(称为单位网格)都是全等的。为了方便,将\(A,B,C,D\)都增加\(10^9\),因为\(10^9\bmod4=10^9\bmod2=0\),所以图形没有变化。(很重要,这......
  • ABC354
    A.ExponentialPlant模拟代码实现h=int(input())now,day=0,0whilenow<=h:now+=1<<dayday+=1print(day)B.AtCoderJanken2模拟代码实现#include<bits/stdc++.h>#definerep(i,n)for(inti=0;i<(n);++i)usingname......