Codeforces Edu Round 49 A-E

A. Palindromic Twist

The need to change. So make \ (A [I] = A [n-- I +. 1] \) .

Before either go to the same, but must satisfy \ (A [I] = A [n-- I +. 1] \) .

Either meet, must meet the difference between the two characters \ (2 \) distance.

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 110;
int n;
char str[N];
bool judge(){
    for(int i = 1; i <= n / 2; i++)
        if(abs(str[i] - str[n - i + 1]) != 2 && abs(str[i] - str[n - i + 1]) != 0) return false;
    return true;
}
int main(){
    int T; scanf("%d", &T);
    while(T--){
        scanf("%d%s", &n, str + 1);
        if(judge()) puts("YES");
        else puts("NO");
    }
    return 0;
}

B. Numbers on the Chessboard

Questions to find the law, if \ (n-\) is odd, then each row possible \ ((n - 1) / 2 \) or \ ((n + 1) / 2 \) numbers, the partition may be considered:

#include <iostream>
#include <cstdio>
#define int long long
using namespace std;
int n, q;
signed main(){
    scanf("%lld%lld", &n, &q);
    for(int i = 1; i <= q; i++){
        int x, y; scanf("%lld%lld", &x, &y);
        int num = 0;
        if(n % 2){
            if((x + y) % 2 == 0){
                if(x % 2) num = (x - 1) * n / 2 + y / 2 + 1;
                else num = (x - 2 + 1) * n / 2 + 1 + y / 2;
            }else{
                if(x % 2) num = n * n / 2 + 1 + (x - 1) * n / 2 + y / 2;
                else num = n * n / 2 + (x - 2 + 1) * n / 2 + 1 + y / 2 + 1;
            }
        }else{
            if((x + y) % 2 == 0){
                if(x % 2) num = (x - 1) * (n / 2) + y / 2 + 1;
                else num = (x - 1) * (n / 2) + y / 2;
            }else{
                if(x % 2) num = n * n / 2 + (x - 1) * (n / 2) + y / 2;
                else num = n * n / 2 + (x - 1) * (n / 2) + y / 2 + 1;
            }
        }
        printf("%lld\n", num);
        
    }
    return 0;
}

C. Minimum Value Rectangle

Disposed rectangle sides of length \ (A \) , \ (B \) , and \ (A <= B \) , then:

\(\frac{P ^ 2}{S} = \frac{(2(a + b)) ^ 2}{a * b}= \frac{4a ^ 2 + 4b ^ 2 + 8ab }{a * b} = 4(\frac{a}{b} + \frac{b}{a}) + 8\)

So \ (\ frac {a} { b} + \ frac {b} {a} \) minimum, just so \ (\ frac {b} { a} \) minimum both because \ (\ frac {b {A}}> =. 1 \) , but \ (\ FRAC {A} {B} <=. 1 \) , so that the latter has no effect on the sum.

#include <iostream>
#include <cstdio>
#include <limits.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int SIZE = 10010;
int n, a, cnt[SIZE], d[SIZE << 1], tot, ansA, ansB;
double c, minn;
 
int main(){
    int T; scanf("%d", &T);
    while(T--){
        memset(cnt, 0, sizeof cnt);
        bool success = false; tot = 0;
        minn = 10001;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a), cnt[a]++;
            if(cnt[a] == 2) d[++tot] = a;
            if(cnt[a] == 4) d[++tot] = a;
        } 
        sort(d + 1, d + 1 + tot);
        for(int i = 1; i < tot; i++){
            if(d[i] == d[i + 1]){ ansA = d[i], ansB = d[i + 1]; break; }
            c = (double)d[i + 1] / d[i];
            if(c < minn)ansA = d[i], ansB = d[i + 1], minn = c;
        }    
        printf("%d %d %d %d\n", ansA, ansA, ansB, ansB);
    }
    return 0;
}

D. Mouse Hunt

For each block Unicom, the selector ring (including sub-rings) disposed in a position sandwiched either rat.

In qualifying position and takes the minimum value is the answer.

When selecting a plurality of points, which may prove greater than the cost of a selected point.

If selected green dot, select 1 + 20, but in order to ensure that each room can get rid of mice, so 2000 is mandatory, so the choice of spending Unicom fast multiple points> Select only one point.

#include <iostream>
#include <cstdio>
#include <vector>
#include <limits.h>
using namespace std;
const int N = 200010;
int n, c[N], a[N], ans;
bool st[N], vis[N];
vector<int> G[N], edge, val;

void dfs(int u){
    if(vis[u]){
        val.push_back(u);
        while(edge.size() && edge.back() != u)
            val.push_back(edge.back()), edge.pop_back();
        return ;
    }
    vis[u] = true, edge.push_back(u), dfs(a[u]);
}
void mark(int u){
    for(int i = 0; i < G[u].size(); i++){
        int v = G[u][i];
        if(!st[v]) st[v] = true, mark(v);
    }
}
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", c + i);
    for(int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        G[i].push_back(a[i]);
        G[a[i]].push_back(i);
    } 
    for(int i = 1 ; i <= n; i++){
        if(!st[i]){
            val.clear(); edge.clear();
            dfs(i);
            int res = INT_MAX;
            for(int i = 0; i < val.size(); i++)
                res = min(res, c[val[i]]);
            ans += res;
            st[i] = true, mark(i);
        }
    }
    printf("%d", ans);
    return 0;
}

E. Inverse Coloring

Autistic, after reading a little understanding of the problem solution a little bit.

Set \ (f [i] [j ] [k] \) of length \ (I \) , the maximum number of consecutive fill \ (K \) , the tail of the longest continuous coating color number of up to \ (J \) the number of programs

It is contemplated that this state can be extended to the state are:

  • When a new color remains the same as before the end, \ (F [I +. 1] [+ J. 1] [max (. 1 + J, K)] \)
  • Is not the same, \ (F [I +. 1] [. 1] [max (. 1, K)] \)

\ (cnt [i] \) is actually the length \ (i \) of all the number of programs, so that they meet:

\ (I * J <K \) , \ (CNT [I] * CNT [J] \) can be added to the answer

…...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 510, mod = 998244353;
typedef long long LL;
int n, k, f[2][N][N], cnt[N];
void update(int &a, int b){ a = (a + b) % mod; }
int main(){
    scanf("%d%d", &n, &k);
    f[0][0][0] = 1;
    for(int i = 0; i < n; i++){
        int pre = i & 1, now = pre ^ 1;
        memset(f[now], 0, sizeof f[now]);
        for(int j = 0; j <= n; j++){
            for(int k = 0; k <= n; k++){
                update(f[now][j + 1][max(j + 1, k)], f[pre][j][k]);
                update(f[now][1][max(1, k)], f[pre][j][k]);
            }
        }  
    }
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= n; j++){
            update(cnt[i], f[n & 1][j][i]);
        }
    }
    LL ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i * j < k)
                ans = (ans + (LL)cnt[i] * cnt[j]) % mod;
        }
    }
    printf("%lld", (ans / 2) % mod);
    return 0;
}

Guess you like

Origin www.cnblogs.com/dmoransky/p/11260524.html