您的位置:首页 > 其它

Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A B 水 搜索

2017-02-21 21:56 525 查看
A. Oath of the Night's Watch

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

"Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come." — The Night's Watch oath.

With that begins the watch of Jon Snow. He is assigned the task to support the stewards.

This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him.

Can you find how many stewards will Jon support?

Input
First line consists of a single integer n (1 ≤ n ≤ 105) — the number of stewards with Jon Snow.

Second line consists of n space separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) representing the values assigned to the stewards.

Output
Output a single integer representing the number of stewards which Jon will feed.

Examples

Input
2
1 5


Output
0


Input
3
1 2 5


Output
1


Note
In the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5.

In the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 2.

题意:给你n个数 输出去掉最小值最大值的之后的数列的个数

题解:水

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll  __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace  std;
int n;
int a[100005];
int main()
{
scanf("%d",&n);
int minx=1000000001;
int maxn=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
minx=min(minx,a[i]);
maxn=max(maxn,a[i]);
}
int ans=0;
if(minx==maxn){
printf("0\n");
return 0;
}
else
{
for(int i=1;i<=n;i++)
{
if(a[i]==minx)
ans++;
if(a[i]==maxn)
ans++;
}
}
printf("%d\n",n-ans);
return 0;
}


B. Code For 1

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility.

Initially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position

,

,

sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.

Now the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?

Input
The first line contains three integers n, l, r (0 ≤ n < 250, 0 ≤ r - l ≤ 105, r ≥ 1, l ≥ 1) – initial element and the range l to r.

It is guaranteed that r is not greater than the length of the final list.

Output
Output the total number of 1s in the range l to r in the final sequence.

Examples

Input
7 2 5


Output
4


Input
10 3 10


Output
5


Note
Consider first example:



Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.

For the second example:



Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5.

题意:给你一个数x 分成{x/2,x%2,x/2} 直到每一项为0或1 问你[l,r]内1的个数。

题解:对一个三叉树dfs dp[x]表示 x分解之后的数列的长度

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll  __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace  std;
ll n,l,r;
map<ll,ll> dp;
ll dfs(ll x)
{
if(x!=0)
dp[x]+=2*dfs(x/2)+1;
return dp[x];
}
int main()
{
dp[1]=1;
scanf("%I64d %I64d %I64d",&n,&l,&r);
dp.clear();
ll m=n;
dfs(m);
ll re=0;
for(ll i=l; i<=r; i++)
{
ll exm=n;
ll ans=0;
while(exm)
{
if(ans+dp[exm/2]+1==i)
{
if(exm%2==1)
re++;
break;
}
else
{
if(ans+dp[exm/2]>i)
exm/=2;
else
{
if(ans+dp[exm/2]==i)
{
re++;
break;
}
ans+=dp[exm/2]+1;
}
}
if(ans==i)
{
re++;
break;
}
}
}
printf("%I64d\n",re);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐