• LOJ#6085. 「美团 CodeM 资格赛」优惠券(set)


    题意

    题目链接

    Sol

    考虑不合法的情况只有两种:

    1. 进去了 再次进去

    2. 没进去 但是出来了

    显然可以用未知记录抵消掉

    直接开个set维护一下所有未知记录的位置

    最优策略一定是最后一次操作位置的后继

    同时我们需要记录一下每个人是否在里面

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e6 + 10;
    inline int read() {
    	char c = getchar(); int x = 0, f = 1;
    	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return x * f;
    }
    int M, las[MAXN], op[MAXN];//las上一次进去的时间  op最后一次操作的位置 
    set<int> ms;
    signed main() {
    	//freopen("a.in", "r", stdin);
    	M = read();
    	for(int i = 1; i <= M; i++) {
    		char c = 'g';
    		while(c != 'I' && c != 'O' && c != '?') c = getchar();
    		if(c == 'I') {
    			int x = read();
    			if(las[x]) {//在里面 
    				auto pos = ms.upper_bound(op[x]);
    				if(pos == ms.end() || (*pos > i)) return printf("%d", i), 0;
    				else ms.erase(pos);
    			}
    			las[x] = 1;
    			op[x] = i;
    		} else if(c == 'O') {
    			int x = read();
    			if(!las[x]) {
    				auto pos = ms.upper_bound(op[x]);
    				if(pos == ms.end() || (*pos > i)) return printf("%d", i), 0;
    				else ms.erase(pos);
    			}
    			las[x] = 0;
    			op[x] = i;
    		} else ms.insert(i);
    		c = 'g';
    	}
    	puts("-1");
    	return 0;
    }
    
  • 相关阅读:
    天下第一 (spfa判断环)
    网络的可靠性 (最小生成树)
    星际之门(一) (快幂+最小生成树)
    吝啬的国度 建图+深搜
    表达式求值 第九届河南省省赛
    Apple Tree (树形结构变为线性结构+树状数组)
    士兵杀敌(五)(线段树??)
    动物统计加强版
    Supermarket
    生活的烦恼
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10320183.html
Copyright © 2020-2023  润新知