zoukankan      html  css  js  c++  java
  • 小a的排列

    链接:https://ac.nowcoder.com/acm/contest/317/G
    来源:牛客网

    小a有一个长度为nn的排列。定义一段区间是"萌"的,当且仅当把区间中各个数排序后相邻元素的差为11
    现在他想知道包含数x,yx,y的长度最小的"萌"区间的左右端点

    也就是说,我们需要找到长度最小的区间[l,r][l,r],满足区间[l,r][l,r]是"萌"的,且同时包含数xx和数yy
    如果有多个合法的区间,输出左端点最靠左的方案。
     
     
    说实话:这道题我还没搞懂
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n, x, y;
    int pre[100005];
    int main()
    {
        int l, r;
        cin >> n >> x >> y;
        for (int i = 1; i <= n; ++i)
        {
            cin >> pre[i];
            if (pre[i] == x)l = i;
            if (pre[i] == y)r = i;
        }
        if (l > r)swap(l, r);
        int xx = 0, yy = n + 1;
        while (r - l != xx - yy){
            for (int i = l; i <= r; ++i){
                xx = max(xx, pre[i]);
                yy = min(yy, pre[i]);
            }
            for (int i = 1; i <= n; ++i){
                if (pre[i] > yy&&pre[i] < xx&&i < l)l = i;
                if (pre[i]>yy&&pre[i]<xx&&i>r)r = i;
            }
        }
        cout << l << " "<<r << endl;
    }
  • 相关阅读:
    Counting Stars hdu
    Color it hdu
    steins;Gate
    原根
    3-idiots
    Tree
    洛谷P1352 没有上司的舞会
    洛谷P1131 时态同步
    洛谷P3177 树上染色
    Codeforces Round #617 (Div. 3)
  • 原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/10327422.html
Copyright © 2011-2022 走看看