477. Doors
Time limit per test: 1.5 second(s)
Memory limit: 65536 kilobytes
Memory limit: 65536 kilobytes
input: standard
output: standard
output: standard
It seems that the Museum of the Modern Technology is the only place where they don't celebrate Halloween! How can they be so far from all the mistical and mysterious? You can hardly believe that two weeks ago the Museum staff have destroyed the last detail that could bring with its ominous creak a bit of Halloween atmosphere to the realm of technology. They have replaced the old wooden doors with new automatic ones, and now they are scratching their heads over how to configure the doors.
By the order of the Director, two automatic doors were purchased. An automatic door is characterized by parameter t , called the
![](http://acm.sgu.ru/cache/d647aad3f46e13f009ba5cd566985688.png)
More formally, two lists of time moments are given. At the moments p 1 < p 2 <... < p n people have passed through the first door, and at the moments q 1 < q 2 <... < q m people have passed through the second one. The task is to use the given statistics to find the optimal
![](http://acm.sgu.ru/cache/9e804c9d6a54ea1886c634da747bbe2c.png)
- The total number of openings of the doors must be minimal possible.
- There is no continuous interval of time that both doors are opened during this interval and its length exceeds the given value d.
InputThe first line of the input contains three integers n, m and d (1 ≤ n, m ≤ 5000, 1 ≤ d ≤ 109). The second line contains numbers pi, and the third line contains numbers qi, given in the ascending order (1 ≤ pi, qi ≤ 109).
OutputOutput two integers t1 and t2, separated by a single space. If there are multiple solutions, output any. If there is no solution, output "No solution
".
Example(s)sample input
sample output
3 2 4 1 6 13 7 11
3 2
题意:两个门分别设定t1,t2两个值,使得满足某些条件。
题解:首先,门的t值的一定为1或者(相邻两个间隔+1) /2。那么窝们可以枚举。但是判断两个门的公共时间是最少是o(n)的,那么如果n^2枚举坑定会跪。那么仔细一看其实o(n)枚举,给出源代码。
#include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<iostream> #include<vector> #include<queue> #include<map> #include<set> #include<stack> #define msn(x) (memset((x),0,sizeof((x)))) #define msx(x) (memset((x),0x7f,sizeof((x)))) #define fuck(x) cerr << #x << " <- " << x << endl #define acer cout<<"sb"<<endl typedef int ll; using namespace std; int n,m,d; const int maxn=5003; int p[maxn],q[maxn]; vector<int>cmp,cmq; struct node { ll a,b; }; vector<node>t,s; node g; int ansa,ansb,ans; node mp(ll a,ll b) { node re; re.a=a,re.b=b; return re; } void pre1(int w) { s.clear(); for(int i=0;i<n;i++) { ll beg=p[i]-w; while(i+1<n&&p[i+1]-2*w<=p[i])i++; ll fin=p[i]+w; s.push_back(mp(beg,fin)); } } void pre2(int w) { t.clear(); for(int i=0;i<m;i++) { ll beg=q[i]-w; while(i+1<m&&q[i+1]-2*w<=q[i])i++; ll fin=q[i]+w; t.push_back(mp(beg,fin)); } } inline ll getintersection(node &a,node &b) { ll minn=max(a.a,b.a); ll maxx=min(a.b,b.b); return max(0,maxx-minn); } bool solve(int a,int b) { pre2(cmq[b]); int j=0; for(int i=0;i<s.size();i++) { g=s[i]; while(j<t.size()) { ll ans=getintersection(g,t[j]); if(ans>d)return 0; if(j==t.size()-1||s[i].b<t[j].b)break; j++; } } if(s.size()+t.size()<ans) { ansa=cmp[a]; ansb=cmq[b]; ans=s.size()+t.size(); } return 1; } int main() { scanf("%d %d %d",&n,&m,&d); ans=10000000; for(int i=0;i<n;i++) { scanf("%d",&p[i]); } for(int i=0;i<m;i++) { scanf("%d",&q[i]); } for(int i=0;i+1<n;i++) { cmp.push_back((p[i+1]+1-p[i])/2); } for(int i=0;i+1<m;i++) { cmq.push_back((q[i+1]+1-q[i])/2); } cmp.push_back(1); cmq.push_back(1); sort(cmp.begin(),cmp.end()); cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end()); sort(cmq.begin(),cmq.end()); cmq.erase(unique(cmq.begin(),cmq.end()),cmq.end()); pre1(1);pre2(1); if(!solve(0,0)) { printf("No solution\n"); return 0; } int sz=cmq.size()-1; for(int i=0;i<cmp.size();i++) { pre1(cmp[i]); while(sz>=0&&!solve(i,sz))sz--; if(sz<0)break; } printf("%d %d\n",ansa,ansb); return 0; }