zoukankan      html  css  js  c++  java
  • 2020牛客寒假算法基础集训营4-I 匹配星星【贪心】

     链接:https://ac.nowcoder.com/acm/contest/3005/I
    来源:牛客网

    示例1

    输入

    复制
    2
    1 1 0
    2 2 1

    输出

    复制
    1
    示例2

    输入

    复制
    2
    1 1 1
    2 2 1

    输出

    复制
    0

    备注:

     思路:

      

      1 #include <bits/stdc++.h>
      2 #define dbg(x) cout << #x << "=" << x << endl
      3 
      4 using namespace std;
      5 typedef long long LL;
      6 
      7 template<class T>inline void read(T &res)
      8 {
      9     char c;T flag=1;
     10     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
     11     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
     12 }
     13 
     14 namespace _buff {
     15     const size_t BUFF = 1 << 19;
     16     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
     17     char getc() {
     18         if (ib == ie) {
     19             ib = ibuf;
     20             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
     21         }
     22         return ib == ie ? -1 : *ib++;
     23     }
     24 }
     25 
     26 int qread() {
     27     using namespace _buff;
     28     int ret = 0;
     29     bool pos = true;
     30     char c = getc();
     31     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
     32         assert(~c);
     33     }
     34     if (c == '-') {
     35         pos = false;
     36         c = getc();
     37     }
     38     for (; c >= '0' && c <= '9'; c = getc()) {
     39         ret = (ret << 3) + (ret << 1) + (c ^ 48);
     40     }
     41     return pos ? ret : -ret;
     42 }
     43 
     44 const int maxn = 1e5 + 7;
     45 
     46 set <int> s;
     47 map<int, int> mapp;
     48 
     49 int n;
     50 
     51 struct node {
     52     int x,y,z;
     53 }a[maxn];
     54 
     55 bool cmp(node a, node b) {
     56     if(a.x == b.x) {
     57         return a.z > b.z;
     58     }
     59     return a.x < b.x;
     60 }
     61 
     62 int main()
     63 {
     64     read(n);
     65     for(int i = 1; i <= n; ++i) {
     66         scanf("%d %d %d",&a[i].x, &a[i].y, &a[i].z);
     67     }
     68     sort(a+1, a+n+1, cmp);
     69     set<int>::iterator it;
     70 
     71     LL ans = 0;
     72     for(int i = 1; i <= n; ++i) {
     73         if(a[i].z == 0) {
     74             if(!mapp[a[i].y]) {
     75                 s.insert(a[i].y);
     76             }
     77             mapp[a[i].y]++;
     78         }
     79         if(a[i].z == 1) {
     80             it = s.lower_bound(a[i].y);
     81             if(it != s.begin()) {
     82                 mapp[*it]--;
     83                 if(!mapp[*it]) {
     84                     s.erase(*it);
     85                 }
     86                 dbg(*it);
     87                 ans++;
     88             }
     89         }
     90     }
     91     cout << ans << endl;
     92     return 0;
     93 }
     94 /*
     95 5
     96 1 1 0
     97 2 2 0
     98 3 3 0
     99 4 4 0
    100 5 5 1
    101 */
    View Code

  • 相关阅读:
    Redis3.2集群部署安装
    熟悉SQL Server 数据类型
    泛型的参数简介和参数约束
    流(Stream)与文件流(FileStream)
    C#提取双引号中的字符串
    数组,一维数组,二维数组,交错数组
    结构,枚举
    C#中唯一的三元运算符
    位运算
    异常处理语句
  • 原文地址:https://www.cnblogs.com/orangeko/p/12297453.html
Copyright © 2011-2022 走看看