2020牛客寒假算法基础集训营4-I 匹配星星【贪心】
链接:https://ac.nowcoder.com/acm/contest/3005/I
来源:牛客网
备注:
思路:
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 */