zoukankan      html  css  js  c++  java
  • 洛谷 P2073 送花

    这题其实可以用vector水掉!

    定义:

    记住要用结构体(c为价格,x为美丽值)!

    以c排序。

    struct Node {
        int x,c;
        bool operator < (const &Node t) const {
            return c<t.c;
        }
    }
    vector <Node> a;
    

    插入:

    upper_bound的作用是将当前节点插到合适位置,以达到排序效果。

    记住不能插入同样价格的花。

    所以用vis[]数组统计。

    if (vis[c]) continue;
    vis[c]=1;
    a.insert(upper_bound(a.begin(),a.end(),(Node){x,c}),(Node){x,c});
    
    

    删除:

    大节点就删除最后一个,记住要把vis[]清掉。

    vector区间是左闭右开的,所以删除erase(a.end()-1)。

    即:

    if (opt==2) vis[a[a.size()-1].c]=0,a.erase(a.end()-1);
    

    小节点就删除第一个,即:

    if (opt==3) vis[a[0].c]=0,a.erase(a.begin());
    

    代码:

    注意要开long long~

    #include <bits/stdc++.h>
    using namespace std;
    typedef int _int;
    #define int long long
    struct Node {
        int x,c;
        bool operator < (const Node &t) const {
            return c<t.c;
        }
    };
    vector <Node> a;
    int ans1,ans2;
    bool vis[1000001];
    _int main()
    {
        int opt,x,c;
        while (1) {
            scanf("%lld",&opt);
            if (opt==-1) break;
            if (opt==1) {
                scanf("%lld%lld",&x,&c);
                if (vis[c]) continue;
                vis[c]=1;
                a.insert(upper_bound(a.begin(),a.end(),(Node){x,c}),(Node){x,c});
            }
            if (!a.size()) continue;
            if (opt==3) vis[a[0].c]=0,a.erase(a.begin());
            if (opt==2) vis[a[a.size()-1].c]=0,a.erase(a.end()-1);
        }
        int len=a.size();
        for (int i=0;i<len;++i) ans1+=a[i].x,ans2+=a[i].c;
        cout<<ans1<<' ';
        cout<<ans2<<endl;
        return 0;
    }
    
  • 相关阅读:
    [栈]
    [数据结构实验]学生成绩管理
    [数据结构实验]集合交并
    shapefile 转 geojson 文件类型
    ubuntu sublime text key
    opengl
    c++
    sublime text3 key
    ubuntu安装nvidia驱动
    全球国家svg边界svg
  • 原文地址:https://www.cnblogs.com/fushao2yyj/p/9602769.html
Copyright © 2011-2022 走看看