题目大意:给出一个序列,有几个操作,询问相邻两个数的差值的绝对值的最小值,排序后差值绝对值的最小值。
思路:简单用平衡树或者set水一下就行了。
(我个沙茶最开始sort_min还用的set维护
CODE:
#include <set> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 500010 #define INF 0xf3f3f3f using namespace std; int cnt,asks; int src[MAX],last[MAX]; multiset<int> sorted,list_min; int sort_min = INF; char s[20]; int main() { cin >> cnt >> asks; for(int i = 1; i <= cnt; ++i) scanf("%d",&src[i]),last[i] = src[i],sorted.insert(src[i]); for(int i = 2; i <= cnt; ++i) list_min.insert(abs(src[i] - src[i - 1])); multiset<int>::iterator l; for(multiset<int>::iterator it = sorted.begin(); it != sorted.end(); ++it) { if(it != sorted.begin()) sort_min = min(sort_min,abs(*it - *l)); l = it; } for(int x,y,i = 1; i <= asks; ++i) { scanf("%s",s); if(s[0] == 'I') { scanf("%d%d",&x,&y); multiset<int>::iterator it = sorted.insert(y); it--,sort_min = min(sort_min,abs(y - *it)),it++; it++,sort_min = min(sort_min,abs(y - *it)),it--; it = list_min.find(abs(last[x] - src[x + 1])); list_min.erase(it); list_min.insert(abs(y - last[x])); list_min.insert(abs(src[x + 1] - y)); last[x] = y; } else if(s[4] == 'S') printf("%d\n",sort_min); else printf("%d\n",*list_min.begin()); } return 0; }
时间: 2024-10-14 20:30:59