AtCoder Beginner Contest 217 D - Cutting Woods & E - Sorting Queries

D - Cutting Woods

D题的话很简单,只需要维护区间的左右边长即可。我们使用set来存储被切割的点,每切一刀就把点加入集合。搜寻长度就upper_bound一下set,取当前和前一个数的差即可(set默认自动排序)

#include<bits/stdc++.h>
using namespace std;
//================================= 
set<int> S;
int l,m;
//=================================
int main(){
    cin >> l >> m;   
    S.insert(0);
    S.insert(l); 
    
    for(int i=1;i<=m;i++){
    	int op,x;
    	cin >> op >> x;
    	if(op==1){
    		S.insert(x);
    	} 
    	else{
    		auto pos = S.upper_bound(x);
    		cout << *pos - *(--pos) << endl;
    	}
    }
	return 0;
}

E - Sorting Queries

E的话突然想到一个很妙的方法,用小根堆+手写队列维护;
如果是1插入操作,只需要先插入队列即可
如果是2弹出打印操作,如果小根堆为空就打印队列的开头,并弹出队列。否则就打印小根堆的开头并弹出
三操作只需要把队列的所有元素加进小根堆即可
目前在提交的里面还没看到比我快的36ms

#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
using namespace std;
//================================
#define debug(a) cout << #a": " << a << endl;
#define N 200010
//================================
typedef pair<int,int> pii;
#define x first
#define y second
typedef long long LL; typedef unsigned long long ULL; typedef long double LD;
inline LL read() { LL s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(LL x, int op = 10) { if (!x) { putchar('0'); if (op)  putchar(op); return; }  char F[40]; LL tmp = x > 0 ? x : -x; if (x < 0)putchar('-');  int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';     tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]);    if (op) putchar(op); }
//================================= 

int q;
int op,x;
priority_queue<int,vector<int>,greater<int>> heap;
int a[2*N],hh=1,tt=0;
//=================================
int main(){
    q=read();
    while(q--){
    	op=read();
    	if(op==1){
    		x=read();
    		a[++tt]=x;
    	}
    	else if(op==2){
    		if(heap.empty()){
    			print(a[hh++]);
    		}
    		else{
    			print(heap.top());
    			heap.pop();
    		}
    	}
    	else{
    		while(hh<=tt) heap.push(a[hh++]);
    	}
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值