首页 > 其他分享 >ABC365

ABC365

时间:2024-08-04 10:05:35浏览次数:6  
标签:int rep wj ABC365 print using dp

A. Leap Year

模拟

代码实现
import calendar
y = int(input())
if calendar.isleap(y):
    print(366)
else:
    print(365)

B. Second Best

模拟

代码实现
n = int(input())
a = list(map(int, input().split()))
print(a.index(sorted(a)[-2])+1)

C. Transportation Expenses

二分

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;

int main() {
    int n; ll m;
    cin >> n >> m;
    
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    
    auto f = [&](int x) {
        ll s = 0;
        rep(i, n) s += min(x, a[i]);
        return s <= m;
    };
    
    const int INF = 1001001001;
    if (f(INF)) puts("infinite");
    else {
        int ac = 0, wa = INF;
        while (abs(ac-wa) > 1) {
            int wj = (ac+wa)/2;
            if (f(wj)) ac = wj; else wa = wj;
        }
        cout << ac << '\n';
    }
    
    return 0;
}

D. AtCoder Janken 3

如果你考虑“从前到后遍历能赢就赢”的贪心的话,会发现过不了样例3
考虑dp
dp[i][j] 表示到第 \(i\) 次操作为止且最后一次出的是 \(j\) 时所能获胜的最大次数

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

int main() {
    int n;
    string s;
    cin >> n >> s;
    
    const int INF = 1001001001001;
    vector dp(n+1, vector<int>(3, -INF));
    rep(i, 3) dp[0][i] = 0;
    for (int i = 1; i <= n; ++i) {
        int x = 0;
        if (s[i-1] == 'R') x = 0;
        if (s[i-1] == 'P') x = 1;
        if (s[i-1] == 'S') x = 2;
        
        rep(j, 3) {
            int val = 0;
            if (j == (x+1)%3) val = 1;
            if (j == (x+2)%3) continue;
            rep(pj, 3) {
                if (j == pj) continue;
                dp[i][j] = max(dp[i][j], dp[i-1][pj]+val);
            }
        }
    }
    
    int ans = ranges::max(dp[n]);
    cout << ans << '\n';
    
    return 0;
}

标签:int,rep,wj,ABC365,print,using,dp
From: https://www.cnblogs.com/Melville/p/18341494

相关文章

  • ABC365(A-D)未补
    A-LeapYear(模拟)题意:给定一个数字n,如果n不是4的倍数,输出365;如果n是4的倍数但不是100的倍数,输出366;如果n是100的倍数但不是400的倍数,输出365;如果n是400的倍数,输出366分析:模拟题目即可代码:#include<bits/stdc++.h>usingnamespacestd;intmain(){intn;cin>>n;......