Educational Codeforces Round 157 (Rated for Div. 2)

Educational Codeforces Round 157 (Rated for Div. 2)

A

模拟

#include 

using namespace std;

const int N = 3e5 + 10;


void solve()
{
    int x , y , k;
    cin >> x >> y >> k;
    if(y <= x){
        cout << x << endl;
    }else if(x + k >= y){
        cout << y << endl;
    }else if(x + k < y){
        cout << y + (y - x - k) << endl;
    }
}

int main()
{
    int T = 1;
    cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

B

贪心排序

#include 

using namespace std;

const int N = 3e5 + 10;
int a[N];

void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= 2 * n; i++)
    {
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n + n);
    int ans = 0;
    for (int i = 2; i <= n; i++)
    {
        ans += (a[i] - a[i - 1]) + (a[i + n] - a[i + n - 1]);
    }
    cout << ans << endl;
    for (int i = 1; i <= n; i++)
    {
        cout << a[i] << " " << a[i + n] << endl;
    }
}

int main()
{
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

C

枚举所有可能的长度

#include 

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::array<std::vector<std::string>, 6> f;
    for (int i = 0; i < n; i++) {
        std::string s;
        std::cin >> s;
        
        f[s.size()].push_back(s);
    }
    i64 ans = 0;
    for (int x = 1; x <= 5; x++) {
        for (int y = 1; y <= 5; y++) {
            if ((x + y) % 2 == 0) {
                std::array<int, 100> cnt{};
                int h = (x + y) / 2;
                for (auto a : f[x]) {
                    int s = 50;
                    for (int i = 0; i < x; i++) {
                        if (i < h) {
                            s += a[i] - '0';
                        } else {
                            s -= a[i] - '0';
                        }
                    }
                    cnt[s] += 1;
                }
                for (auto a : f[y]) {
                    int s = 50;
                    for (int i = 0; i < y; i++) {
                        if (x + i >= h) {
                            s += a[i] - '0';
                        } else {
                            s -= a[i] - '0';
                        }
                    }
                    ans += cnt[s];
                }
            }
        }
    }
    std::cout << ans << "\n";
    
    return 0;
}

你可能感兴趣的:(算法,算法,c++)