【t065】最敏捷的机器人

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

【背景】
Wind设计了很多机器人。但是它们都认为自己是最强的,于是,一场比赛开始了~
【问题描述】
机器人们都想知道谁是最敏捷的,于是它们进行了如下一个比赛。
首先,他们面前会有一排共n个数,它们比赛看谁能最先把每连续k个数中最大和最小值写下来,当然,这些机器人运算速度都很快,
它们比赛的是谁写得快。
但是Wind也想知道答案,你能帮助他吗?
【输入格式】

每组测试数据
第1行为n,k(1<=k<=n<=100000)
第2行共n个数,为数字序列,所有数字均在longint范围内。

【输出格式】

共n-k+1行
第i行为第i~i+k-1这k个数中的最大和最小值
Sample Input

5 3
1 2 3 4 5

Sample Output

3 1
4 2
5 3

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t065

【题解】

线段树记录一下区间的最大值最小值即可;

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n,k;
int ma[MAXN<<2],mi[MAXN<<2],a[MAXN<<2];

void build(int l,int r,int rt)
{
    if (l==r)
    {
        scanf("%d",&a[rt]);
        mi[rt] = ma[rt] = a[rt];
        return;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    ma[rt] = max(ma[rt<<1],ma[rt<<1|1]);
    mi[rt] = min(mi[rt<<1],mi[rt<<1|1]);
}

int q_ma(int L,int R,int l,int r,int rt)
{
    if (L<=l && r<= R)
        return ma[rt];
    int m = (l+r)>>1;
    int temp1 = -21e8,temp2 = -21e8;
    if (L<=m)
        temp1 = q_ma(L,R,lson);
    if (m<R)
        temp2 = q_ma(L,R,rson);
    return max(temp1,temp2);
}

int q_mi(int L,int R,int l,int r,int rt)
{
    if (L<=l && r<= R)
        return mi[rt];
    int m = (l+r)>>1;
    int temp1 = 21e8,temp2 = 21e8;
    if (L<=m)
        temp1 = q_mi(L,R,lson);
    if (m<R)
        temp2 = q_mi(L,R,rson);
    return min(temp1,temp2);
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);rei(k);
    build(1,n,1);
    rep1(i,1,n-k+1)
    {
        int L = i,R=i+k-1;
        printf("%d %d\n",q_ma(L,R,1,n,1),q_mi(L,R,1,n,1));
    }
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(312)  评论(0编辑  收藏  举报