Codeforces C. Minimum Grid Path (Round 106 Rated for Div. 2) (simulate enumeration/prefix (and & minimum))

Portal

Meaning of the question: You need to start from the origin (0, 0) to the end point (n, n) in the first quadrant of the plane. You can only walk an integer number of steps to the right or up, and change the direction at most n-1 times in the process . Now tell you the average cost of each of the n segments, try to find the minimum consumption to get to the end.

Ideas:

Since the direction is changed at most n-1 times, we can use the cost of n segments.

So when we enumerate the last segment, it ends at the i-th time (to the end). Note: It is assumed that the distance of the i-1 segment before this is changed in alternate directions.

For odd positions, the sum of the total distances must be exactly equal to n, so we find the odd position i and the minimum of all previous odd positions, let it take the most steps, and only take one step at the rest of the positions. The same is true for even positions. Keep updating the answer to get the minimum value.

Code:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
   
   {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9+7;
const int  N = 2e5 + 5;

inline void read(int &x){
    char t=getchar();
    while(!isdigit(t)) t=getchar();
    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}

int t, n, c[N];
int ma[N], mb[N], pra[N], prb[N];

signed main()
{
    IOS;

    cin >> t;
    while(t --){
        cin >> n;
        me(ma); me(mb);
        me(pra); me(prb);
        for(int i = 1; i <= n; i ++){
            cin >> c[i];
            if(i&1){
                if(i==1) ma[i] = pra[i] = c[i];
                else ma[i] = min(ma[i-2], c[i]), pra[i] = pra[i-2]+c[i];
            }
            else{
                if(i==2) mb[i] = prb[i] = c[i];
                else mb[i] = min(mb[i-2], c[i]), prb[i] = prb[i-2]+c[i];
            }
        }
        int ans = inf;
        for(int i = 2; i <= n; i ++){
            int tmp = 0, k = i/2;
            if(i&1){
                tmp += prb[i-1]+(n-k)*mb[i-1];
                tmp += pra[i]+(n-k-1)*ma[i];
            }
            else{
                tmp += prb[i]+(n-k)*mb[i];
                tmp += pra[i-1]+(n-k)*ma[i-1];
            }
            ans = min(ans, tmp);
//            cout << "i: " << i << " tmp: " << tmp << endl;
        }
        cout << ans << endl;
    }

    return 0;
}

Java exercise: I forgot to define tmp as long type wa4 during the period, and then if all arrays are defined as long type, if they are all of 1e5+10 size, it will be t3.

import java.util.Scanner;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while(t>0) {
            t --;
            int n = in.nextInt();
            long [] c = new long[n+10];
            long [] ma = new long[n+10];
            long [] mb = new long[n+10];
            long [] pra = new long[n+10];
            long [] prb = new long[n+10];
            for(int i = 1; i <= n; i ++){
                c[i] = in.nextLong();
                if(i%2!=0){
                    if(i==1) ma[i] = pra[i] = c[i];
                    else {
                        ma[i] = Math.min(ma[i-2], c[i]);
                        pra[i] = pra[i-2]+c[i];
                    }
                }
                else{
                    if(i==2) mb[i] = prb[i] = c[i];
                    else{
                        mb[i] = Math.min(mb[i-2], c[i]);
                        prb[i] = prb[i-2]+c[i];
                    }
                }
            }
            long ans = 1L<<60;
            for(int i = 2; i <= n; i ++){
                long tmp = 0, k = i/2;
                if(i%2!=0){
                    tmp += prb[i-1]+(n-k)*mb[i-1];
                    tmp += pra[i]+(n-k-1)*ma[i];
                }
                else{
                    tmp += prb[i]+(n-k)*mb[i];
                    tmp += pra[i-1]+(n-k)*ma[i-1];
                }
                ans = Math.min(ans, tmp);
            }
            System.out.println(ans);
        }
        in.close();
    }
}

 

Guess you like

Origin blog.csdn.net/Satur9/article/details/115003661