[Offer收割]编程练习赛39

Posted PoorLitt1eThin9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Offer收割]编程练习赛39相关的知识,希望对你有一定的参考价值。

公平分队

技术分享图片
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;



int cmp(const void * x, const void * y) {
#define datatype int
    datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
    //x < y
    return dx < dy ? -1 : 1;
#undef datatype
}

int a[200005];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < 2 * n; i++) cin >> a[i];
    sort(a, a + 2 * n);
    lint ans = 0;
    for (int i = 1; i < n; i++) ans += a[i];
    ans += a[2 * n - 1];
    cout << ans << endl;
    return 0;
}
View Code

XY游戏

技术分享图片
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;



int cmp(const void * x, const void * y) {
#define datatype int
    datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
    //x < y
    return dx < dy ? -1 : 1;
#undef datatype
}

map<int, int> mp;
struct state {
    int a[4][4];
    int step;
};
queue<state> q;
int hash_state(state st) {
    int rtn = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++)
            rtn = rtn * 3 + st.a[i][j];
    }
    return rtn;
}
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(0), cin.tie(0);
    state initial, st, stt;
    char ch;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            cin >> ch;
            while (ch != O && ch != X && ch != Y) cin >> ch;
            if (ch == O) initial.a[i][j] = 0;
            else if (ch == X) initial.a[i][j] = 1;
            else initial.a[i][j] = 2;
        }
    }
    initial.step = 0;
    q.push(initial);
    mp[hash_state(initial)]++;
    while (!q.empty()) {
        st = q.front();
        q.pop();
        bool ok = false;
        for (int i = 0; i < 4; i++) {
            if (st.a[i][0] == 1 && st.a[i][1] == 1 && st.a[i][2] == 1 && st.a[i][3] == 1) ok = true;
            if (st.a[i][0] == 2 && st.a[i][1] == 2 && st.a[i][2] == 2 && st.a[i][3] == 2) ok = true;
            if (st.a[0][i] == 1 && st.a[1][i] == 1 && st.a[2][i] == 1 && st.a[3][i] == 1) ok = true;
            if (st.a[0][i] == 2 && st.a[1][i] == 2 && st.a[2][i] == 2 && st.a[3][i] == 2) ok = true;
            if (st.a[0][0] == 1 && st.a[1][1] == 1 && st.a[2][2] == 1 && st.a[3][3] == 1) ok = true;
            if (st.a[0][0] == 2 && st.a[1][1] == 2 && st.a[2][2] == 2 && st.a[3][3] == 2) ok = true;
            if (st.a[0][3] == 1 && st.a[1][2] == 1 && st.a[2][1] == 1 && st.a[3][0] == 1) ok = true;
            if (st.a[0][3] == 2 && st.a[1][2] == 2 && st.a[2][1] == 2 && st.a[3][0] == 2) ok = true;
        }
        if (ok) {
            cout << st.step << endl;
            return 0;
        }
        stt = st;
        stt.step = st.step + 1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (st.a[i][j] == 0) continue;
                for (int k = 0; k < 4; k++) {
                    if (0 <= i + dx[k] && i + dx[k] < 4 && 0 <= j + dy[k] && j + dy[k] < 4 && st.a[i + dx[k]][j + dy[k]] == 0) {
                        stt.a[i + dx[k]][j + dy[k]] = stt.a[i][j];
                        stt.a[i][j] = 0;
                        int h = hash_state(stt);
                        if (mp.find(h) == mp.end()) {
                            mp[h]++;
                            q.push(stt);
                        }
                        stt.a[i][j] = stt.a[i + dx[k]][j + dy[k]];
                        stt.a[i + dx[k]][j + dy[k]] = 0;
                    }
                }
            }
        }
    }
    cout << -1 << endl;
    return 0;
}
View Code

第K小最简真分数

前缀后缀查询

以上是关于[Offer收割]编程练习赛39的主要内容,如果未能解决你的问题,请参考以下文章

[Offer收割]编程练习赛42

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)

[Offer收割]编程练习赛32

[Offer收割]编程练习赛63

[Offer收割]编程练习赛40

[Offer收割]编程练习赛57