920

He went to the water question time, the subject of more water, AK whole lot, as a side dish of chicken I had 210 points. .

 

A swap is a question I have, time complexity is $ O (n) $, and the range of n <1000, so that too much water. We can expect the same situation to steal the same as the rest of the money, and when the first pocket stealing money will decrease, so we enumerator to the first pocket of the rest of the money to enumerate through the for loop the remaining amount of money, and then sum denoted SUM2, original sum1-sum2 ans sequence and compared, taking max is the final answer, time is about $ O (n * a [1]) $, about 1e7 , we have been able over. But I also thought of a $ O (n) $ algorithm, we assume that the first pocket stole the money of x, and due to the same, then the second plus pocket money on x, the third pocket is decremented to the money x ... is easy to see, and when n is an even number, the thief can not steal the money, and when n is odd, the number of take min odd numbered pocket money, the final output can Save

#include <bits/stdc++.h> 

using namespace std;

//typedef long long ll;
const int INF = 0x3f3f3f3f;
//const int MAXN = 5e5 + 100;
//const int MAXM = 3e3 + 10;
//const double eps = 1e-5;

template < typename T > inline void read(T &x) {
    x = 0; T ff = 1, ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') ff = -1;
        ch = getchar();
    }
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar(); 
    }
    x *= ff;
}

template < typename T > inline void write(T x) {
    if(x == 0) {
        putchar('0');
        return ;
    }
    if(x < 0) putchar('-'), x = -x;
    static T tot = 0, ch[20];
    while(x) {
        ch[++tot] = x % 10 + '0';
        x /= 10;
    } 
    while(tot) putchar(ch[tot--]);
}
    
int n, x, ans = INF;

int main() {
    freopen("thief.in", "r", stdin);
    freopen("thief.out", "w", stdout);
    read(n);
    for(int i = 1; i <= n; ++i) {
        read(x);
        if(i & 1) ans = min(ans, x);
    }    
    --ans;
    if((n & 1) == 0) ans = 0;
    write(ans);
    return 0;
}
Correct

 

 

When solving the problem, good search to 80 points, as the number of columns strictly monotonically increasing, and are positive integers, so the smallest situation is 1, 2 ..., n, we enumerate every i, when a [ i]> a [i - 1], we have two options, one is the skip this point, the search continues down, there is the a [i] to be assigned a [i - 1] + 1, continue down, when the a [i] $ \ le $ a [i - 1] when the a [i] to be assigned a [i - 1] + 1, continued downward, the time complexity is between $ O (n ) $ to $ O (2 ^ n) $ between, but it is quite good. Positive solution, we may assume that b [i] = a [i] - i; when b is the array is not decreased, A array is incremented, so we just determined b the array monotonically decreased sequences len, n - len output can, in particular, if b [i] <0, the point which will be modified, can be skipped when looking for sequences.

#include <bits/stdc++.h> 

using namespace std;

//typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e4 + 100;
//const int MAXM = 3e3 + 10;
//const double eps = 1e-5;

template < typename T > inline void read(T &x) {
    x = 0; T ff = 1, ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') ff = -1;
        ch = getchar();
    }
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar(); 
    }
    x *= ff;
}

template < typename T > inline void write(T x) {
    if(x == 0) {
        putchar('0');
        return ;
    }
    if(x < 0) putchar('-'), x = -x;
    static T tot = 0, ch[20];
    while(x) {
        ch[++tot] = x % 10 + '0';
        x /= 10;
    } 
    while(tot) putchar(ch[tot--]);
}
    
int T, n, ans, a[MAXN], f[MAXN];
int flag = false;

inline void dfs(int x, int val) {
    if(val >= ans) return ;
    if(x == n + 1) {
        ans = min(ans, val);
        return ;
    }
    if(a[x] > a[x - 1]) {
        dfs(x + 1, val);
        if(a[x] - a[x - 1] > 1) {
            int u = a[x];
            a[x] = a[x - 1] + 1;
            dfs(x + 1, val + 1);
            a[x] = u;
        }
    }
    else {
        int u = a[x];
        a[x] = a[x - 1] + 1;
        dfs(x + 1, val + 1);
        a[x] = u;
    }
    return ;
}

int main() {
    freopen("noname.in", "r", stdin);
    freopen("noname.out", "w", stdout);
    read(T);
    while(T--) {
        read(n);
        flag = false;
        ans = INF;
        for(register int i = 1; i <= n; ++i) {
            read(a[i]); 
            if(a[i] <= a[i - 1]) flag = true;
        }
        if(!flag) {
            puts("0");
            continue;
        }
        dfs(1, 0);
        write(ans);
        puts("");
        
    } 
    return 0;
}
search for
#include <bits/stdc++.h> 

using namespace std;

//typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e4 + 100;
//const int MAXM = 3e3 + 10;
//const double eps = 1e-5;

template < typename T > inline void read(T &x) {
    x = 0; T ff = 1, ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') ff = -1;
        ch = getchar();
    }
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar(); 
    }
    x *= ff;
}

template < typename T > inline void write(T x) {
    if(x == 0) {
        putchar('0');
        return ;
    }
    if(x < 0) putchar('-'), x = -x;
    static T tot = 0, ch[20];
    while(x) {
        ch[++tot] = x % 10 + '0';
        x /= 10;
    } 
    while(tot) putchar(ch[tot--]);
}
    
int T, n, ans, len, a[MAXN], b[MAXN], f[MAXN];

int main() {
    freopen("noname.in", "r", stdin);
    freopen("noname.out", "w", stdout);
    read(T);
    while(T--) {
        read(n);
        for(register int i = 1; i <= n; ++i) {
            read(a[i]); 
            b[i] = a[i] - i;
        }
        memset(f, 0, sizeof(f));
        ans = 1;
        f[1] = b[1]; 
        for(register int i = 2; i <= n; ++i) {
            if(b[i] < 0) continue;
            if(b[i] >= f[ans]) f[++ans] = b[i];
               else f[upper_bound(f + 1, f + ans + 1, b[i]) - f] = b[i];
             
        }
        write(n - ans);
        puts(""); 
    } 
    return 0;
}
Correct

 

 

emmmmm, or search for it, to enumerate each point of which employees will answer to min, time complexity is $ O (3 ^ n) $, had lost 30 points. Think positive solution is actually very good, is dp, shortest seek first Floyd, let f [i] [j] [k] to complete the remaining tasks before i spend a minimum two people in the j and k, then clearly

    f [i] [j] [k] = min (f [i] [j] [k], b [i - 1 ] [j] [k] + a [b [i - 1 ]] [b [i ]]); 
    f [i] [b [i - 1 ]] [k] = min (f [i] [b [i - 1 ]] [k], b [i - 1 ] [j] [k] + a [j ][bee]]); 
    f [i] [b [i - 1 ]] [j] = min (f [i] [b [i - 1 ]] [j], f [i - 1 ] [j] [k] + a [k ][bee]]);

We can set f [0] [1] [2] = f [0] [2] [1] = 0, b [0] = 3, the rest is INF, the target state is f [l] [?] [? ] minimum, $ O (l * n ^ 2) $ transferred and taken enough min.

 

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e2 + 100;
const int MAXM = 2e2 + 10;
const double eps = 1e-5;

template  < typename T > inline void read(T &x) {
    x = 0; T ff = 1, ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') ff = -1;
        ch = getchar();
    }
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar(); 
    }
    x *= ff;
} 

template < typename T > inline void write(T x) {
    if(x == 0) {
        putchar('0');
        return ;
    }
    if(x < 0) putchar('-'), x = -x;
    static T tot = 0, ch[20];
    while(x) {
        ch[++tot] = x % 10 + '0';
        x /= 10;
    } 
    while(tot) putchar(ch[tot--]);
}

int T, n, l, ans, b[MAXN];
int a[MAXM][MAXM], f[MAXN][MAXM][MAXM];

inline void Floyd() {
    for(register int k = 1; k <= n; ++k) 
        for(register int i = 1; i <= n; ++i) 
            for(register int j = 1; j <= n; ++j) 
                a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
}

int main() {
    freopen("service.in", "r", stdin);
    freopen("service.out", "w", stdout);
    read(T);
    while(T--) { 
        read(n); read(l); 
        for(register int i = 1; i <= n; ++i) {
            for(register int j = 1; j <= n; ++j) {
                read(a[i][j]);
            }
        }
        for(register int i = 1; i <= l; ++i) {
            read(b[i]);
        }
        Floyd();
        memset(f, 0x3f, sizeof(f));
        f[0][1][2] = 0;
        f[0][2][1] = 0;
        b[0] = 3;
        for(int i = 1; i <= l; ++i) {
            for(int j = 1; j <= n; ++j) {
                for(int k = 1; k <= n; ++k) {
                    int s = b[i - 1], t = b[i];
                    f[i][j][k] = min(f[i][j][k], f[i - 1][j][k] + a[s][t]);
                    f[i][s][k] = min(f[i][s][k], f[i - 1][j][k] + a[j][t]);
                    f[i][s][j] = min(f[i][s][j], f[i - 1][j][k] + a[k][t]);
                } 
            }
        }
        ans = INF;
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= n; ++j) {
                ans = min(ans, f[l][i][j]);
            } 
        }
        write(ans);
        puts("");
    }
    return 0;
} 
Correct

 


 

Guess you like

Origin www.cnblogs.com/AK-ls/p/11568073.html
920