The Famous ICPC Team Again
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1155 Accepted Submission(s): 561
Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
5 5 3 2 4 1 3 1 3 2 4 3 5 5 10 6 4 8 2 3 1 3 2 4 3 5
Case 1: 3 3 2 Case 2: 6 6 4
题意:
给你一串数字表示题目的难度,题目已经按时间顺序排好,每次找出时间在[s,t]之间,
且难度为中间值的题目,而且保证了给的查找范围是奇数,所以变成了每次查找[s,t]中,
第(t-s)/2+1大的难度为多少
#include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <Map>
using namespace std;
typedef long long ll;
typedef long double ld;
using namespace std;
const int maxn = 100010;
int tree[20][maxn];
int sorted[maxn];
int toleft[20][maxn];
void build(int l,int r,int dep)
{
if(l == r)
return;
int mid = (l+r)>>1;
int same = mid-l+1;
for(int i = l;i <= r;i++)
{
if(tree[dep][i] < sorted[mid])
same--;
}
int lpos = l;
int rpos = mid+1;
for(int i = l;i <= r;i++)
{
if(tree[dep][i] < sorted[mid])
tree[dep+1][lpos++] = tree[dep][i];
else if(tree[dep][i] == sorted[mid] && same > 0)
{
tree[dep+1][lpos++] = tree[dep][i];
same --;
}
else
tree[dep+1][rpos++] = tree[dep][i];
toleft[dep][i] = toleft[dep][l-1] + lpos -l;
}
build(l,mid,dep+1);
build(mid+1,r,dep+1);
}
int query(int L,int R,int l,int r,int dep,int k)
{
if(l == r)
return tree[dep][l];
int mid = (L+R)>>1;
int cnt = toleft[dep][r]-toleft[dep][l-1];
if(cnt >= k)
{
int lpos = L+toleft[dep][l-1]-toleft[dep][L-1];
int rpos = lpos+cnt-1;
return query(L,mid,lpos,rpos,dep+1,k);
}
else
{
int rpos = r+toleft[dep][R]-toleft[dep][r];
int lpos = rpos-(r-l-cnt);
return query(mid+1,R,lpos,rpos,dep+1,k-cnt);
}
}
int main()
{
int n,m;
int cas = 1;
while(scanf("%d",&n) != EOF)
{
memset(tree,0,sizeof(tree));
for(int i = 1;i <= n;i++)
{
scanf("%d",&sorted[i]);
tree[0][i] = sorted[i];
}
sort(sorted+1,sorted+n+1);
build(1,n,0);
scanf("%d",&m);
int l,r;
printf("Case %d:\n",cas++);
while(m--)
{
scanf("%d%d",&l,&r);
int k =(r-l)/2+1;
printf("%d\n",query(1,n,l,r,0,k));
}
}
return 0;
}
198



被折叠的 条评论
为什么被折叠?



