168522025-05-14 08:28:08TortelliniJrÖsszegzésekcpp17Accepted 100/10018ms2088 KiB
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    long long n, s, x, t, u;
    vector<long long> a, b, o;
    s = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        a.push_back(x);
        b.push_back(x);
        o.push_back(x);
        s += x;
    }
    //find one higher
    //method: go from end, find smallest number that neighbors one that is bigger, increment it and save its location
    //but: never can be the last number: cant add ones
    //so we start later
    //also end before 0: dont want overindexing
    //if none found: increment first
    //solution: copy everything until location, then spam 1 until sum = s
    if (o.size() != 1)
    {
        long long z = s - a[a.size() - 1];
        int loc = 0;
        for (int i = a.size() - 2; i > 0; i--)
        {
            if (a[i] < a[i - 1])
            {
                loc = i;
                break;
            }
            else
            {
                z -= a[i];
            }
        }
        a[loc]++;
        z++;
        a.resize(loc + 1);
        for (int i = 0; i < s - z; i++)
        {
            a.push_back(1);
        }
    }
    else
    {
        a = { 0 };
    }
    //find one lower (earlier)
    //method: drop first non-one value by one, proceed to append one to the last number we can "legally" add to, if none then append zero
    //sum is same, but lexicographically lower now
    long long l = -1;
    for (int i = b.size() - 1; i >= 0; i--)
    {
        if (b[i] != 1)
        {
            b[i]--;
            l = i;
            break;
        }
    }
	if (l != -1)
	{
        bool flag = false;
        for (int i = l + 1; i < b.size(); i++)
        {
            if (b[i] < b[i - 1])
            {
                b[i]++;
                flag = true;
                break;
            }
        }
        if (!flag)
        {
            b.push_back(1);
        }
	}
    else
    {
        b = { 0 };
    }
    //output time!
    for (int i = 0; i < a.size(); i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    if (b != o)
    {
        for (int i = 0; i < b.size(); i++)
        {
            cout << b[i] << " ";
        }
    }
    else
    {
        cout << "0";
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Accepted1ms316 KiB
3Accepted1ms316 KiB
subtask26/6
4Accepted1ms316 KiB
5Accepted1ms316 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted1ms316 KiB
subtask36/6
9Accepted1ms500 KiB
10Accepted1ms316 KiB
11Accepted1ms316 KiB
12Accepted4ms820 KiB
13Accepted4ms824 KiB
subtask410/10
14Accepted1ms500 KiB
15Accepted1ms316 KiB
16Accepted1ms316 KiB
17Accepted4ms820 KiB
18Accepted4ms824 KiB
19Accepted1ms316 KiB
20Accepted2ms316 KiB
21Accepted17ms1840 KiB
22Accepted18ms2088 KiB
23Accepted17ms1836 KiB
subtask510/10
24Accepted2ms508 KiB
25Accepted2ms316 KiB
26Accepted2ms316 KiB
27Accepted2ms316 KiB
28Accepted1ms424 KiB
subtask610/10
29Accepted1ms316 KiB
30Accepted1ms316 KiB
31Accepted1ms316 KiB
32Accepted1ms316 KiB
33Accepted1ms316 KiB
subtask758/58
34Accepted1ms316 KiB
35Accepted1ms316 KiB
36Accepted1ms316 KiB
37Accepted1ms316 KiB
38Accepted1ms316 KiB
39Accepted1ms316 KiB
40Accepted1ms316 KiB
41Accepted1ms316 KiB
42Accepted1ms500 KiB
43Accepted1ms316 KiB
44Accepted1ms316 KiB
45Accepted4ms820 KiB
46Accepted4ms824 KiB
47Accepted1ms316 KiB
48Accepted2ms316 KiB
49Accepted17ms1840 KiB
50Accepted18ms2088 KiB
51Accepted1ms424 KiB
52Accepted1ms316 KiB
53Accepted1ms316 KiB
54Accepted1ms316 KiB
55Accepted1ms316 KiB
56Accepted1ms316 KiB
57Accepted1ms316 KiB
58Accepted1ms364 KiB
59Accepted1ms316 KiB
60Accepted1ms500 KiB
61Accepted1ms316 KiB
62Accepted2ms316 KiB
63Accepted2ms316 KiB
64Accepted2ms368 KiB
65Accepted3ms564 KiB
66Accepted4ms820 KiB