[hihocoder][Offer收割]编程练习赛58

最大的K-偏差排列

每次取可选范围里的最大的数字,如果最左侧的数字还没有使用就直接使用最左侧的数字

#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<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;

void makedata() {
    freopen("input.txt", "w", stdout);
    fclose(stdout);
}


int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n, k;
    bool f[200];
    cin >> n >> k;
    memset(f, false, sizeof(f));
    for (int i = 1; i <= n; i++) {
        if (i - k > 0 && !f[i - k]) {
            f[i - k] = true;
            cout << i - k << ' ';
            continue;
        }
        for (int j = i + k; j > i - k; j--) {
            if (j > n || j < 0) continue;
            if (!f[j]) {
                f[j] = true;
                cout << j << ' ';
                break;
            }
        }
    }
    return 0;
}
View Code

孤独的字符

枚举每个字符,计算它是一个孤独字符时包含它的区间有多少个

#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<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI;

void makedata() {
    freopen("input.txt", "w", stdout);
    fclose(stdout);
}

string s;
int pre[110000], nex[110000], last[256];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    cin >> s;
    int n = s.length();
    memset(last, -1, sizeof(last));
    for (int i = 0; i < n; i++) {
        pre[i] = last[s[i]];
        nex[i] = n;
        if (last[s[i]] >= 0) nex[pre[i]] = i;
        last[s[i]] = i;
    }
    lint ans = 0;
    for (int i = 0; i < n; i++) {
        ans += 1LL * (nex[i] - i) * (i - pre[i]);
    }
    cout << ans << endl;
    return 0;
}
View Code

秋天来了

这题有问题,都说了l是唯一最高完了样例好几个最高???最高的还能看到别人???

Nim森林

这题跟这棵树没啥关系。两个回合以后,是一个典型的反nim游戏,先手获胜的条件为:1)所有堆的石子个数为1,且NIM_sum=0或2)至少有一堆的石子个数大于1,且 NIM_sum≠0。第一种获胜情况是所有堆狮子个数为1且有偶数堆,这种情况是不可能达到的。经过第一回合操作后,只要有石子数为1的石堆,B都可以拿完多于1的石堆且剩下奇数个石子数为1的石堆,所以只需考虑第二种情况(且所有石堆石子数大于1)。按代价从大到小的顺序添加各个石堆的石子数到线性基中,成功添加进去的就是第二回合给B留下的。

#include <bits/stdc++.h>
using namespace std;
using LL = int64_t;
using LD = long double;
const LL INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;

template <typename T>
struct LnBase{
    int sz, szc;
    T *x;
    int *y;
    LnBase (){x = 0; sz = sizeof(T) << 3; szc = -1; resize(sz);}
    void resize(int size){
        sz = size; if(!x) delete(x); x = new T[sz + 2]; y = new int[sz + 2];
        memset(x, 0, sz * sizeof(T)); memset(y, 0, sz << 2);
    }
    T operator[](int h){return x[h];}
    int add(T v){
        for(int i = sz - 1; i >= 0; i--)
            if(v & (T)1 << i){if(!x[i]){x[i] = v; szc = -1; return i;} v ^= x[i];}
        return -1;
    }
    int find(T v){
        for(int i = sz - 1; i >= 0; i--){
            if(v & (T)1 << i && x[i]) v ^= x[i]; if(!v) return 1;}
        return 0;
    }
    T Max(){
        T s = 0;
        for(int i = sz - 1; i >= 0; i--){if((s ^ x[i]) > s) s ^= x[i];}
        return s;
    }
    T Min(){
        for(int i = 0; i < sz; i++) if(x[i]) return x[i];
        return -1;
    }
    void Canonicity(){
        int i, j;
        for(i = sz - 1; i > 0; i--)
            for(j = i - 1; j >= 0; j--) if(x[i] & (T)1 << j) x[i] ^= x[j];
        for(szc = i = 0; i < sz; i++) if(x[i]) y[szc++] = i;
    }
    T Kth(long long K){
        if(szc < 0) Canonicity(); if(K >= 1ll << szc) return -1; T s = 0;
        for(int i = szc - 1; i >= 0; i--) if(K & 1ll << i) s ^= x[y[i]];
        return s;
    }
};

struct node {
    __int128 a, t;
    bool operator<(const node &e) const { return t > e.t; }
};
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    n--;
    vector<node> a(n);
    for (auto &i : a) {
        LL u, v, a, t;
        cin >> u >> v >> a >> t;
        i.a = a;
        i.t = u ^ v ^ t;
    }
    sort(a.begin(), a.end());
    int flag = 1;
    __int128 ans = 0, sum = 0;
    LnBase<__int128> b;
    for (auto& s : a) {
        sum += s.t;
        if (s.a == 1) continue;
        flag = 0;
        if (b.add(s.a) != -1) ans += s.t;
    }
    if (flag) cout << "No
";
    else {
        ans = sum - ans;
        string s = "";
        while (ans) {
            s += '0' + ans % 10;
            ans /= 10;
        }
        if (s == "") s = "0";
        reverse(s.begin(), s.end());
        cout << s << '
';
    }
}
View Code
原文地址:https://www.cnblogs.com/dramstadt/p/9107485.html