A. Gabriel and Caterpillar



time limit per test



memory limit per test



input



output


9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.

a cm per hour by day and slips down byb

10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2pm.

Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.


Input



h1, h2 (1 ≤ h1 < h2 ≤ 105) — the heights of the position of the caterpillar and the apple in centimeters.

a, b (1 ≤ a, b ≤ 105) — the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.


Output



k

 - 1.


Examples



input



10 30 2 1



output



1



input



10 13 1 1



output



0



input



10 19 1 2



output



-1



input



1 50 5 4



output



1


Note



10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6

Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.




大意:毛毛虫爬树摘苹果,白天一小时爬acm,晚上每小时掉bcm,奇葩的是。有个人想看毛毛虫摘苹果,而且只能在每天下午2点之后才可以看到,问需要等待多少天可以看到毛毛虫摘苹果。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int h1,h2,a,b;
int main()
{
while(~scanf("%d%d%d%d",&h1,&h2,&a,&b))
{
if(a<=b)
{
if(h1+8*a<h2)
{
puts("-1");
continue;
}
}
if(h1+8*a>=h2)
{
puts("0");
continue;
}
int hig=h2-h1-8*a;
int sp=(a-b)*12;
int ans=hig/sp;
if(hig%sp)
ans++;
printf("%d\n",ans);
}
return 0;
}


题目链接:​​点击打开链接​


B. z-sort



time limit per test



memory limit per test



input



output


z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold:

  1. aiai- 1 for all eveni,
  2. aiai- 1 for all oddi> 1.

[1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.

z-sorted?


Input



n (1 ≤ n ≤ 1000) — the number of elements in the array a.

n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.


Output



a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".


Examples



input



4 1 2 2 1



output



1 2 1 2



input



5 1 3 2 2 5



output



1 5 2 3 2


思路:可以看出起伏线排列就是一个合法序列,不存在 impossible 的可能

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n;
int a[1010];
int b[1010];
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",a+i);
sort(a+1,a+n+1);
queue<int> sma;
queue<int> big;
int cnt=n>>1;
for(int i=n;i>n-cnt;i--)
// for(int i=n-cnt+1;i<=n;i++)
big.push(a[i]);
if(n&1) cnt++;
for(int i=1;i<=cnt;i++)
sma.push(a[i]);
int num=0;
while(sma.size()&&big.size())
{
b[num++]=sma.front();
sma.pop();
b[num++]=big.front();
big.pop();
}
if(!sma.empty())
{
b[num++]=sma.front();
sma.pop();
}
for(int i=0;i<num;i++)
printf(i==num-1?"%d\n":"%d ",b[i]);
}
return 0;
}


题目链接:​​点击打开链接​


C. Foe Pairs



time limit per test



memory limit per test



input



output


p of length n. Also you are given m foe pairs (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi).

(x, y) (1 ≤ x ≤ y ≤ n) that do not contain any foe pairs. So you shouldn't count intervals (x, y)

p = [1, 3, 2, 4] and foe pairs are {(3, 2), (4, 2)}. The interval (1, 3) is incorrect because it contains a foe pair (3, 2). The interval (1, 4) is also incorrect because it contains two foe pairs (3, 2) and (4, 2). But the interval (1, 2)


Input



n and m (1 ≤ n, m ≤ 3·105) — the length of the permutation p

n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p.

m lines contains two integers (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi) — the i-th foe pair. Note a foe pair can appear multiple times in the given list.


Output



c — the number of different intervals (x, y)

64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long


Examples



input



4 2 1 3 2 4 3 2 2 4



output



5



input



9 5 9 7 2 3 1 4 6 5 8 1 6 4 5 2 7 7 2 2 7



output



20


Note



(1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).




大意:题目给出一个1到n的某一个排列,有m个二维点。问这n个数形成的所有子区间中,不包含m个点中任何一个的子区间有多少?

#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
int n,m;
int pos[300010];
int dp[300010];
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
pos[x]=i;
dp[i]=INF;
}
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(pos[x]>pos[y])
swap(x,y);
dp[pos[x]]=min(dp[pos[x]],pos[y]);
}
for(int i=n-1;i>=1;i--)
dp[i]=min(dp[i],dp[i+1]);
LL ans=1LL*n*(n-1)/2+n;
for(int i=1;i<n;i++)
{
if(dp[i]!=INF)
ans-=(n-dp[i]+1);
}
printf("%I64d\n",ans);
}
return 0;
}