Codeforces 1499C. Minimum Grid Path (prefix sum, thinking optimization)

Topic link

Title:

Given you a n*n two-dimensional map, let you walk from (0, 0) to (n, n) stipulation:

  1. You can only go to the right and up, and alternate to the right and up.
  2. You can walk several segments each time, and each time you walk there is a price ai a_iai(Cost per unit length) i represents the number of walks
  3. The final total cost is the sum of the total cost of each segment.

Let you seek the smallest price,

Ideas:

I thought of prefixes and optimizations at the beginning, but at the beginning I focused on the last two steps, so that we can ensure that one dimension is optimal, and the other is not optimal. To put it simply, although it is not a positive solution, it can be extracted (refined) from it. That is to say, the maximum we have to walk is 2 N unit length, so we maintain the one-dimensional optimization, but we consider iii time travel. Then we have gonei − 1 i-1altogether in front of usiOnce , let’s push one step forward, which isi − 2 i-2i2 Then we can guarantee that the fronti − 1 i-1i1 times have gone a unit, the last two stepsi and i - 1 i and i-1i sum i1 will definitely reach the end. Then how many steps i-1 takes, the total is 2n steps i-2 steps left(2 ∗ n − i + 2) = sum (2*n-i+2)=sum(2ni+2)=s u m steps first, theni − 1 i-1i1 needs to take (sum+1)/2 steps, i needs to take (sum/2) steps.

This is the unilateral one-dimensional optimal. But we guarantee that one dimension is optimal, but the other dimension is not necessarily optimal. So saying that it is not a correct solution can inspire us to think of a correct solution.

Positive solution: It is the best in two dimensions. We guarantee that the two dimensions are the smallest and the maximum number of steps can be taken to ensure the best final answer. Therefore, the minimum value in two dimensions needs to be maintained. Then we compare each time, record the minimum value, and then maintain the prefix once because each of the previous minimums requires a period of time. We add the number of steps that need to be taken (to the end) when both of the two bits are the minimum, and compare the minimum value.

The following notes are my original misunderstandings. After looking at the understanding, I extracted the correct answer.
Good night, good dreams, everyone.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
typedef long long ll;
ll a[maxn], b[maxn],c[maxn];
ll n, m, cnt, k1, k2;
map<ll, ll> mp;
ll maxx;
string str;
int main()
{
    
    
    ll t;
    cin >> t;
    while(t--)
    {
    
    
        cin >> n;
        b[0]=0;
        for (int i = 1; i <= n; ++i)
        {
    
    
            cin >> a[i];
            b[i] = b[i - 1] + a[i];
        }
        ll ji=a[1],ou=a[2];        
        maxx =n*a[1]+n*ou;
        ll ans=2*n;
        for(int i=3;i<=n;i++){
    
    
            if(i%2)ji=min(ji,a[i]);
            else ou=min(ou,a[i]);
            ll p,q;
            ll sum=ans-i;
            p=(sum+1)/2;
            q=sum/2;
            maxx=min(maxx,b[i]+q*ji+p*ou);
        }



        /*ll ans = 2 * n;
        for(int i = 2; i <= n; i++)
        {
            ll sum = ans - i + 2;
            ll p, q;
            p = sum / 2;
            q = (sum + 1) / 2;
            maxx = min(maxx, b[i - 2] + q * a[i - 1] + p * a[i]);
        }*/
        cout << maxx << endl;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/114994481