题解:P11061 【MX-X4-T1】「Jason-1」IO++

· · 题解

显然,不能去掉输出语句。而输出语句使用的输入必须要被输入。如果一个数被输入,那么这个数前面的数必须输入。为了最小化语句个数,只保留需要用到的输入语句和输出语句。根据上述分析,输入语句可以统计为输出语句使用的最大值。所以,输出语句使用的最大的输入的数加上输出语句的个数即为最终的答案。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, x, maxi, ans; // maxi 统计输出语句使用的最大值,ans 统计输出语句的个数
void solve()
{
    cin >> n;
    while (n--)
    {
        cin >> x;
        if (x)
        {
            maxi = max(maxi, x);
            ans++;
        }
    }
    cout << maxi + ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}