ACM-ICPC 2016 北京赛区现场赛 D,E

题目链接:What a Beautiful lake

/*
 * [链接]:http://hihocoder.com/problemset/problem/1425
 *
 * [题意]:给出长度为n的环形数组,问最长连续递增或递减的区间的长度-1。
 *
 * [分析]:模拟即可。
 *
 * [tricks]:
 *
 * [时间复杂度]:
 *
 * */

#include <bits/stdc++.h>

#define  ll long long

using namespace std;

void scan() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

const int maxn = 1e6 + 7;
int a[maxn], b[maxn];

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    scan();
    int n;
    while (cin >> n && n) {
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            a[n + i] = a[i];
        }
        int ans = 1, tmp = 1, now = 1;
        for (int i = 2; i <= 2 * n; i++) {
            if (a[i] > a[i - 1]) tmp++, ans = max(ans, tmp);
            else tmp = 1;
            if (a[i] < a[i - 1]) now++, ans = max(ans, now);
            else now = 1;
        }
        cout << ans - 1 << endl;
    }
    return 0;
}

题目链接:What a Ridiculous Election 

/*
 * [链接]:http://hihocoder.com/problemset/problem/1426
 *
 * [题意]:给定长度为5的字符串ss,问:由串"12345"变化为串ss,最短的步数。
 *        变化:1.相邻的字符可以交换位置。
 *             2.任意位置+2,%10,最多操作3次。
 *             3.任意位置×2,%10,最多操作两次。
 *
 * [分析]:显然我们直接那样写,由于T很大,因此考虑预处理“12345”到各个可行的数字的解的最短步数。
 * 直接搜索一下就好了。bfs+优先队列
 *
 * [tricks]:
 * 结构体的存储尽量不要用数字,因为你每次搜索的时候,也是需要转换为字符串。
 * 但是当你转换为字符串后又要转换为数字,这样的话,你得到的数字不能保证是5位。
 * 因此直接在结构体中存储当所能到达的串就ok .
 * [时间复杂度]:
 *
 * */

#include <bits/stdc++.h>

#define  ll long long
#define all(x) (x).begin(),(x).end()
using namespace std;

void scan() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

const int maxn = 1e6 + 7;
int ans[maxn][5][5];

const int INF = 0x3f3f3f3f;

struct node {
    string s;
    int op2, op3, step;

    node() {};

    node(string _s, int _op2, int _op3, int _stp) {
        s = _s, op2 = _op2, op3 = _op3, step = _stp;
    }

    friend bool operator<(const node &a, const node &b) {
        return a.step > b.step;
    }

};


int change(string ss) {
    int ret = 0;
    for (int i = 0; i < 5; i++)
        ret = ret * 10 + (ss[i] - '0');
    return ret;
}

void BFS() {
    priority_queue<node> que;
    que.push(node("12345", 0, 0, 0));
    ans[12345][0][0] = 0;
    while (!que.empty()) {
        node now = que.top();que.pop();
        for (int i = 0; i < 4; i++) {
            string zx = now.s;
            swap(zx[i], zx[i + 1]);
            int num = change(zx);
            if (ans[num][now.op2][now.op3] == INF) {
                ans[num][now.op2][now.op3] = now.step + 1;
                que.push(node(zx, now.op2, now.op3, now.step + 1));
            }
        }

        if (now.op2 < 3) {
            for (int i = 0; i < 5; i++) {
                string zx = now.s;
                zx[i] = char(((zx[i] - '0') + 1) % 10 + '0');
                int num = change(zx);
                if (ans[num][now.op2 + 1][now.op3] == INF) {
                    ans[num][now.op2 + 1][now.op3] = now.step + 1;
                    que.push(node(zx, now.op2 + 1, now.op3, now.step + 1));
                }
            }
        }

        if (now.op3 < 2) {
            for (int i = 0; i < 5; i++) {
                string zx = now.s;
                zx[i] = char(((zx[i] - '0') * 2) % 10 + '0');
                int num = change(zx);
                if (ans[num][now.op2][now.op3 + 1] == INF) {
                    ans[num][now.op2][now.op3 + 1] = now.step + 1;
                    que.push(node(zx, now.op2, now.op3 + 1, now.step + 1));
                }
            }
        }
    }
}

int main() {
    memset(ans, 0x3f, sizeof(ans));
    BFS();
    string ss;
    while (cin >> ss) {
        int ret = INF,num=change(ss);
        for (int op2 = 0; op2 <= 3; op2++) {
            for (int op3 = 0; op3 <= 2; op3++)
                ret = min(ret, ans[num][op2][op3]);
        }
        if (ret == INF) cout << -1 << endl;
        else cout << ret << endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值