AtCoder Beginner Contest 163


比賽鏈接:https://atcoder.jp/contests/abc163/tasks

A - Circle Pond

題意

由半徑輸出圓周長。

代碼

#include <bits/stdc++.h>
using namespace std;
int main() {
    double r; cin >> r;
    cout << 2 * 3.14 * r;
}

B - Homework

題意

n 天的假期里要做 m 個任務,最終有多少天假期剩余。

代碼

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m; cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int t; cin >> t;
        n -= t;
    }
    cout << max(-1, n);
}

C - management

題意

統計 n - 1 個 1~n 的數的個數。

代碼

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    int cnt[n] = {};
    for (int i = 0; i < n - 1; i++) {
        int t; cin >> t;
        ++cnt[t - 1];
    }
    for (int i : cnt) cout << i << "\n";
}

D - Sum of Large Numbers

題意

從 10100+0、10100+1、10100+2、...、10100+n 中選取 k、k+1、k+2、...、n、n + 1 個數,問所有選取情況中有多少不同的值。

思路

選 i 個數的值個數 = 選 i 個數的最大值 - 選 i 個數的最小值 + 1。

f(n) - f(n - i) 是選后 i 個數,即選 i 個數的最大值,

f(i - 1) 是選前 i 個數,即選 i 個數的最小值,

ans 初始時為 1 是因為選 n + 1 個數只有一種情況。

代碼

#include <bits/stdc++.h>
#define f(n) ((n + 1LL) * (n) / 2)
using namespace std;
const int mod = 1e9 + 7;
int main() {
    int n, k; cin >> n >> k;
    int ans = 1;
    for (int i = k; i <= n; i++) 
        ans = (ans + f(n) - f(n - i) - f(i - 1) + 1) % mod;
    cout << ans;
}

E - Active Infants

題意

將一個數組重新排序,每個元素的收益為 值 x 變動距離,問排序后的最大收益。 

思路

先排序,然后從大到小分配元素。

dp[i][j] 表示將 i 個元素分配在左邊,j 個元素分配在右邊的最大收益。

代碼

#include <bits/stdc++.h>
using namespace std;
long long dp[2020][2020];
int main() {
    int n; cin >> n;
    int a[n]; for (int &i : a) cin >> i;
    int p[n]; iota(p, p + n, 0);
    sort(p, p + n, [&] (int x, int y) {
        return a[x] > a[y];
    });
    for (int i = 0; i < n; i++) {
        for (int j = 0; i + j < n; j++) {
            int k = i + j;
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + 1LL * a[p[k]] * abs(p[k] - i));
            dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + 1LL * a[p[k]] * abs(p[k] - (n - 1 - j)));
        }
    }
    long long ans = 0;
    for (int i = 0; i <= n; i++) 
        ans = max(ans, dp[i][n - i]);
    cout << ans;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM