Codeforces Edu Round 47

A. Game Shopping

According to both questions is intended to simulate.

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1010;
int n, m, c[N], a[N], ans = 0;
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", c + i);
    for(int i = 1; i <= m; i++) scanf("%d", a + i);
  
    for(int i = 1, j = 1; i <= n; i++)
        if(c[i] <= a[j]) ans ++, j++; 
  
    printf("%d", ans);
    return 0;
}

B. Minimum Ternary String

  • O exchange determines the characteristics of the items 1are free activities.
  • 0And 2the exception is not caught with each other exchange, i.e. the relative positions of 0 and 2 remain unchanged.

Based on the characteristics, can be placed as far forward as one.

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string str;
int main(){
    cin >> str;
    int cnt = 0;
    for(int i = 0; i < str.size(); i++)
        if(str[i] == '1') cnt ++ ;
    
    for(int i = 0; i < str.size(); i++){
        if(str[i] == '1') continue;
        else if(str[i] == '2' && cnt){
            for(int j = 0; j < cnt; j++) printf("1"); cnt = 0; 
        }
        printf("%c", str[i]);
    }
    if(cnt)for(int j = 0; j < cnt; j++) printf("1"); 
    return 0;
}

C. Annoying Present

To make the average of the maximum, you can just let the sum of the maximum.

\ (x * d \) contribution is constant, only concerned \ (\ sum \ limits_ {i = 1} ^ n \ d * | i - j | \) either.

  • If \ (D> 0 \) , then let \ (\ sum \ limits_ {i = 1} ^ n \ | i - j | \) maximum. Obviously \ (I \) takes \ (0 \) or \ (n-\) , the value of the maximum.
  • If \ (D <0 \) , then let \ (\ sum \ limits_ {i = 1} ^ n \ | i - j | \) minimum. Obviously \ (I \) taking the median n $ \ lfloor (n + 1 ) / 2 \ when rfloor $, minimum value.
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 100010;
typedef long long LL;
int n, m, x, d;
LL sum = 0, MAX = 0, MIN = 0;
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++){
        MAX += abs(i - 1);
        MIN += abs(i - (n + 1) / 2);
    }
    for(int i = 1; i <= m; i++){
        int x, d; scanf("%d%d", &x, &d);
        if(d > 0) sum += x * n + d * MAX;
        else sum += x * n + d * MIN;
    }
    printf("%.15f", double(sum) / n);
    return 0;
}

D. Relatively Prime Graph

Violence feasible.

Take any two positive integers, their probability is relatively prime \ (. 6/2 [pi] ^ \) . reference

维基百科: In a sense that can be made precise, the probability that two randomly chosen integers are coprime is \(6/π^2\) (see pi), which is > about 61%. See below.

The most we can simply filter \ (100000/6 / π ^ 2 \ approx 100000/61 \% <200000 \) number, does not time out.

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 100010;
int n, m, tot = 0;
int gcd(int a, int b){
    return b ? gcd(b, a % b) : a;
}
vector<int> G[N];

void gcd_prework(){
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if(gcd(i, j) == 1){
                 ++tot; G[i].push_back(j);
                 if(tot >= m) return; 
            }   
        }
    }
}

int main(){
    scanf("%d%d", &n, &m);

    if(m < n - 1)
        puts("Impossible");
    else{
        gcd_prework();
        if(tot < m) puts("Impossible");
        else{
            puts("Possible");
            for(int i = 1; i <= n; i++)
                for(int j = 0; j < G[i].size(); j++)
                    printf("%d %d\n", i, G[i][j]);
        }
    }
    return 0;
}

E - Intercity Travelling

Back for a long formula autistic, and to find the law questions, step by step toward either the front. The picture came out a little may be better understood.

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 1000010, Mod = 998244353;
int n, a[N], s, g;
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", a + i);
    for(int i = 1; i <= n; i++){
        s = ((LL)s * 2 + a[i] + a[i - 1] + g * 2) % Mod;
        g = ((LL)g * 2 + a[i - 1]) % Mod;
    }
    cout << s;
    return 0;
}

Guess you like

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