标签:
有两种方法
第一种,用set。
开两个set,第一个开multiset保存MIN_GAP的值,并逐步更新。第二个开set保存数列的值,并一步步更新MIN_SORT_GAP。
其实第二个开multiset好像更好写一点?
1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <set> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 vector<int>x[500005]; 9 multiset<int>s; 10 set<int>ss; 11 int main() 12 { 13 // freopen("in.txt","r",stdin); 14 int n,m,ans = 0x3fffffff;scanf("%d%d",&n,&m); 15 for(int i = 1;i<=n;++i){ 16 int t;scanf("%d",&t); 17 x[i].push_back(t); 18 pair<set<int>::iterator,bool> pi = ss.insert(t); 19 if(!pi.second)ans = 0; 20 } 21 22 for(int i = 1;i<n;++i)s.insert(abs(x[i][0]-x[i+1][0])); 23 set<int>::iterator it,iter; 24 for(it = ss.begin();it!=ss.end();it++) 25 { 26 iter = it;iter++; 27 if(iter==ss.end())break; 28 ans = min(ans,abs(*it-*iter)); 29 } 30 while(m--){ 31 char str[20];scanf("%s",str); 32 if(str[0]==‘I‘){ 33 int a,b;scanf("%d%d",&a,&b); 34 if(a==n)s.insert(abs(b-x[a][x[a].size()-1])); 35 else { 36 int t = abs(x[a][x[a].size()-1]-x[a+1][0]); 37 s.erase(s.find(t)); 38 s.insert(abs(b-x[a][x[a].size()-1])); 39 s.insert(abs(b-x[a+1][0])); 40 } 41 x[a].push_back(b); 42 pair<set<int>::iterator,bool> pi = ss.insert(b); 43 if(!pi.second){ans = 0;continue;} 44 it = pi.first; 45 iter = it;iter++; 46 if(iter!=ss.end())ans = min(ans,abs(*it-*iter)); 47 if(it!=ss.begin()){ 48 iter = it;iter--; 49 ans = min(ans,abs(*it-*iter)); 50 } 51 } 52 else { 53 if(str[4]==‘G‘)printf("%d\n",*s.begin()); 54 else printf("%d\n",ans); 55 } 56 } 57 return 0; 58 }
标签:
原文地址:http://www.cnblogs.com/GJKACAC/p/4700170.html