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.
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.
If there is a valid TL value, print it. Otherwise, print -1.
3 6 4 5 2 8 9 6 10 7 11
5
3 1 3 4 5 6
-1
读懂题意很重要,水~
/*************************************************************************
* author:crazy_石头
* algorithm:sort
* date:2013/09/29
* judge status:Accepted
* Time:30ms
* Memory:0KB
* problem:Codeforces Round #203 (Div.2)A. TL
**************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;
#define N 100005
#define M 400010
#define INF INT_MAX
const int maxn=500;
inline int max(int a,int b)
{
return a>b?a:b;
}
inline int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int a[maxn],b[maxn];
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<m;i++)
cin>>b[i];
sort(a,a+n);
sort(b,b+m);
int res=max(2*a[0],a[n-1]);
printf("%d\n",res<b[0]?res:-1);
return 0;
}
Valera's finally decided to go on holiday! He packed up and headed for a ski resort.
Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.
Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.
Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v1, v2, ..., vk (k ≥ 1) and meet the following conditions:
- Objects with numbers v1, v2, ..., vk - 1 are mountains and the object with number vk is the hotel.
- For any integer i (1 ≤ i < k), there is exactly one ski track leading from object vi. This track goes to object vi + 1.
- The path contains as many objects as possible (k is maximal).
Help Valera. Find such path that meets all the criteria of our hero!
The first line contains integer n (1 ≤ n ≤ 105) — the number of objects.
The second line contains n space-separated integers type1, type2, ..., typen — the types of the objects. If typei equals zero, then the i-th object is the mountain. If typei equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel.
The third line of the input contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ n) — the description of the ski tracks. If number ai equals zero, then there is no such object v, that has a ski track built from v to i. If number ai doesn't equal zero, that means that there is a track built from object ai to object i.
In the first line print k — the maximum possible path length for Valera. In the second line print k integers v1, v2, ..., vk — the path. If there are multiple solutions, you can print any of them.
5 0 0 0 0 1 0 1 2 3 4
5 1 2 3 4 5
5 0 0 1 0 1 0 1 2 2 4
2 4 5
4 1 0 0 0 2 3 4 2
1 1
DFS路径长度即可。。
/*************************************************************************
* author:crazy_石头
* algorithm:DFS
* date:2013/09/29
* judge status:Accepted
* Time:218ms
* Memory:8588KB
* problem:Codeforces Round #203 (Div.2)B.Resort
**************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;
#define N 100005
#define M 400010
#define INF INT_MAX
const int maxn=200000+50;
inline int max(int a,int b)
{
return a>b?a:b;
}
inline int min(int a,int b)
{
return a<b?a:b;
}
int a[maxn],b[maxn],n;
int hotel[maxn],mountain[maxn],temp[maxn];//temp数组存储中间结果;
int start[maxn],len[maxn];//start记录始发地,len存储以该点为始发地有多少条相连路径;
void DFS(int curnum,int res)//curnum表示搜索到的当前的序号,res表示加上当前的最终结果;
{
if(len[curnum]>1)
return ;//多于一条路径时返回;
temp[res-1]=curnum;
if(n<res&&(len[start[curnum]]>1||start[curnum]==0))//当搜到下一个点有超过一条路径,或者连接该点的是一条无效路径时就要更新最大值;
{
n=res;
for(int i=0;i<n;i++)
mountain[i]=temp[i];
}
if(start[curnum]!=0&&len[start[curnum]]<=1)
DFS(start[curnum],res+1);
}
int main()
{
int i,num=0,test;
memset(len,0,sizeof(len));
cin>>test;
for(i=1;i<=test;i++)
{
cin>>a[i];
if(a[i]==1)
hotel[num++]=i;
}
for(i=1;i<=test;i++)
{
cin>>b[i];
if(b[i])
{
start[i]=b[i];
len[b[i]]+=1;
}
}
for(i=0;i<num;i++)
{
if(n==0)
{
n=1;
mountain[0]=hotel[i];
}
DFS(hotel[i],1);
}
cout<<n<<endl;
for(i=n-1;i>=0;i--)
{
if(i!=0)
cout<<mountain[i]<<" ";
else
cout<<mountain[i]<<endl;
}
return 0;
}
DE以后补上吧。。。。QAQ