题目链接:点击打开链接
The 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.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down byb cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 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.
The first line contains two integers h1, h2 (1 ≤ h1 < h2 ≤ 105) — the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers a, b (1 ≤ a, b ≤ 105) — the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
Print the only integer k — the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer - 1.
10 30 2 1
1
10 13 1 1
0
10 19 1 2
-1
1 50 5 4
1
In the first example at 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 pm of the same day the caterpillar gets the apple.
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
#include
#include
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("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;
}
题目链接:点击打开链接
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold:
For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.
Can you make the array z-sorted?
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
4 1 2 2 1
1 2 1 2
5 1 3 2 2 5
1 5 2 3 2
#include
#include
#include
#include
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 sma;
queue 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
题目链接:点击打开链接
You are given a permutation p of length n. Also you are given m foe pairs (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi).
Your task is to count the number of different intervals (x, y) (1 ≤ x ≤ y ≤ n) that do not contain any foe pairs. So you shouldn't count intervals (x, y) that contain at least one foe pair in it (the positions and order of the values from the foe pair are not important).
Consider some example: 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) is correct because it doesn't contain any foe pair.
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the length of the permutation p and the number of foe pairs.
The second line contains n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p.
Each of the next 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.
Print the only integer c — the number of different intervals (x, y) that does not contain any foe pairs.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
4 2 1 3 2 4 3 2 2 4
5
9 5 9 7 2 3 1 4 6 5 8 1 6 4 5 2 7 7 2 2 7
20
In the first example the intervals from the answer are (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).
大意:题目给出一个1到n的某一个排列,有m个二维点。问这n个数形成的所有子区间中,不包含m个点中任何一个的子区间有多少?
#include
#include
#include
#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