std & generator

Laffey 2022-11-24 8:58:54 2022-11-24 8:59:39

孩子贺的第 100 道绿题

std

#include <iostream>
using namespace std;

const int MAXN = 1e5 + 10;
int s[MAXN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const char endl = '\n';

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        s[i] += s[i - 1];
    }

    int maxs = 0;
    for (int i = 1, val = 0; i <= n; i++) {
        maxs = max(maxs, s[i] - val);
        val = min(val, s[i]);
    }

    cout << maxs - (s[n] - maxs) << endl;

    return 0;
}

generator

#include <iostream>
#include <fstream>
#include <string>
#include <random>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;

const int MAXN = 1e5 + 10, MOD = 1e4 + 1;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    const char endl = '\n';

    mt19937 rd((random_device()).operator()());
    for (int now = 1; now <= 10; now++) {
        int n = pow(10, ceil((double) now / 2));
        ofstream fout(to_string(now) + ".in", ios_base::out);
        fout << n << endl;
        for (int i = 1; i <= n; i++) {
            fout << rd() % MOD << ' ';
        }
        fout.close();
    }
}