2 seconds
256 megabytes
standard input
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?
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 a single integer representing the number of stewards which Jon will feed.
2
1 5
0
3
1 2 5
1
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[];
int main()
{
scanf("%d",&n);
int minx=;
int maxn=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
minx=min(minx,a[i]);
maxn=max(maxn,a[i]);
}
int ans=;
if(minx==maxn){
printf("0\n");
return ;
}
else
{
for(int i=;i<=n;i++)
{
if(a[i]==minx)
ans++;
if(a[i]==maxn)
ans++;
}
}
printf("%d\n",n-ans);
return ;
}
2 seconds
256 megabytes
standard input
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?
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 the total number of 1s in the range l to r in the final sequence.
7 2 5
4
10 3 10
5
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!=)
dp[x]+=*dfs(x/)+;
return dp[x];
}
int main()
{
dp[]=;
scanf("%I64d %I64d %I64d",&n,&l,&r);
dp.clear();
ll m=n;
dfs(m);
ll re=;
for(ll i=l; i<=r; i++)
{
ll exm=n;
ll ans=;
while(exm)
{
if(ans+dp[exm/]+==i)
{
if(exm%==)
re++;
break;
}
else
{
if(ans+dp[exm/]>i)
exm/=;
else
{
if(ans+dp[exm/]==i)
{
re++;
break;
}
ans+=dp[exm/]+;
}
}
if(ans==i)
{
re++;
break;
}
}
}
printf("%I64d\n",re);
return ;
}