Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it.
Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds).
Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds.
As a result, Valera decided to set v seconds TL, that the following conditions are met:
- v is a positive integer;
- all correct solutions pass the system testing;
- at least one correct solution passes the system testing with some "extra" time;
- all wrong solutions do not pass the system testing;
- value v is minimum among all TLs, for which points 1, 2, 3, 4 hold.
Help Valera and find the most suitable TL or else state that such TL doesn't exist.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
Output
If there is a valid TL value, print it. Otherwise, print -1.
Sample Input
3 6 4 5 2 8 9 6 10 7 11
5
3 1 3 4 5 6
-1
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,m,f,t,p,x,i;
while(cin>>n>>m)
{
f=100;t=1;p=100;
for(i=0;i<n;i++)
{
cin>>x;
if(f>x) f=x;
if(t<x) t=x;//f是正确方案最小的运行时间,t是正确方案的最大的运行时间
}
for(i=0;i<m;i++)
{
cin>>x;
if(p>x) p=x;//p是错误方案最小的运行时间
}
if(p<=t)
cout<<"-1"<<endl;
else
if(2*f>=p)
cout<<"-1"<<endl;
else
{
t=max(t,f*2);
cout<<t<<endl;
}
}
}
题解:
一开始想错了,选的时间v(符合的条件中的最小的),不一定是正确方案运行的最大时间,因为还要保证正确方案运行时间里至少有一个运行时间的二倍小于等于v;
至少有一个,那么正确方案最小的运行时间一定要满足2a<=v;
p<f,输出-1
p<t,输出-1
可以合并小于最小的一定小于最大的
2*f>=p
输出-1
否则输出值