寒假集训比赛题解 DAY2

· · 个人记录

前言

由于 A~D 是老师为了练习给的模板题所以这里不提供题解,而且个人认为本次比赛难度相较于 DAY 1 来说更加正常。

E - P6497 [COCI2016-2017#2] Prosječni

题目传送门

分析题目

首先将 n 分为奇偶两种情况,对其分类讨论。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll n;
ll a[101][101];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);     cout.tie(0);
    cin >> n;
    if(n == 2)
    {
        cout << -1;
        return 0;
    }
    if(n % 2 != 0)
    {
        ll cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cnt++;
                cout << cnt << " ";
            }
            cout << "\n";
        }
        return 0;
    }
    ll cnt = 0;
    for(int i = 1; i < n; i++)
    {
        ll sum = 0;
        for(int j = 1; j < n; j++)
        {
            cnt++;
            sum += cnt;
            a[i][j] = cnt;
            cout << cnt << " ";
        }
        cnt = cnt * n - sum ;
        a[i][n] = cnt;
        cout << cnt << "\n";
    }
    for(int i = 1; i <= n; i++)
    {
        ll sum = 0;
        for(int j = 1; j < n; j++)
            sum += a[j][i];
        a[n][i] = a[n - 1][i] * n - sum;
        cout << a[n][i] << " ";
    }
    return 0;
}

F - P8092 [USACO22JAN] Drought B

题目传送门

分析题目

令人意外的,本题与 E 题一样可以对 n 的奇偶分类讨论。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll n;
ll a[100005];
ll tmp[100005];
bool chk(ll x)
{
    memcpy(tmp, a, sizeof(a));
    for(int i = 1; i <= n; i++)
    {
        if(tmp[i] < x)
            return 0;
        tmp[i + 1] -= tmp[i] - x;
    }
    return 1;
}
ll n_is_even()
{
    ll l = 0, r = 1e9, ans = -1;
    while(l <= r)
    {
        ll mid = l + r >> 1;
        if(chk(mid))
            l = mid + 1, ans = mid;
        else
            r = mid - 1;
    }
    if(ans == -1)
        return -1;
    ll res = 0;
    for(int i = 1; i <= n; i++)
    {
        res += 2 * (a[i] - ans);
        a[i + 1] -= a[i] - ans;
    }
    return res;
}
ll n_id_odd()
{
    ll ans = 0;
    for(int i = 2; i < n; i += 2)
    {
        ll sub = a[i] - a[i - 1];
        if(sub < 0)
            return -1;
        ans += sub * 2;
        a[i + 1] -= sub;
        a[i] -= sub;
    }
    for(int i = n; i >= 3; i--)
    {
        ll sub = a[i - 1] - a[i];
        if(sub < 0)
            return -1;
        ans += sub * 2;
        a[i - 1] -= sub;
        a[i - 2] -= sub;
    }
    for(int i = 1; i < n; i++)
        if(a[i] != a[i + 1] || a[i] < 0 || a[n] < 0)
            return -1;
    return ans;
}
void work()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    if(n == 2)
        cout << (a[1] == a[2] ? 0 : -1) << "\n";
    else if(n % 2 == 0)
        cout << n_is_even() << "\n";
    else
        cout << n_id_odd() << "\n";
    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);     cout.tie(0);
    ll t;
    cin >> t;
    while(t--)
        work();
    return 0;
}