LoginSignup
0
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 261

Posted at

A - Intersection

O(1)
素直にシミュレーションしましょう。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int L1, R1, L2, R2;
    cin >> L1 >> R1 >> L2 >> R2;

    int ans=0;
    rep(i, 101){
        if(L1 < i && i <= R1 && L2 < i && i <= R2){
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
} 

B - Tournament Result

O(N^2)
(x, y)に対象なインデックスは、(y, x) です。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N;
    cin >> N;
    vector<string> A(N);
    rep(i, N) cin >> A[i];

    rep(y, N){
        rep(x, N){
            string str = A[y].substr(x,1) +  A[x].substr(y,1);
            if(!(str == "--" || str == "WL" || str == "LW" || str == "DD")){
                cout << "incorrect" << endl;
                return 0;
            }
        }
    }
    cout << "correct" << endl;

    return 0;
} 

C - NewFolder(1)

O(N)
連想配列を使うと簡単にできます。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N;
    cin >> N;
    vector<string> S(N);
    rep(i, N) cin >> S[i];

    map<string, int> mp;
    rep(i, N){
        if(mp[S[i]] == 0){
            cout << S[i] << endl;
        }else{
            cout << S[i] << "(" << mp[S[i]] << ")" << endl;
        }
        mp[S[i]]++;
    }
    return 0;
} 

D - Flipping and Bonus

O(N^2)
コイントスN回とカウンタでDPの漸化式を作成します。
iがコイントス、jがカウンタ。

漸化式
dp[i][j] = dp[i-1][j-1] + X[i] + Bonus[j];
C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N, M;
    cin >> N >> M;
    vector<ll> X(N+1);
    repx(i, 1, N+1) cin >> X[i];
    vector<ll> Bonus(N+1, 0);
    rep(i, M){
        ll C, Y;
        cin >> C >> Y;
        Bonus[C] = Y;
    }

    vector<vector<ll>> dp(N+1, vector<ll>(N+1, -1));
    dp[0][0] = 0;
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= i; j++) dp[i][j] = dp[i-1][j-1] + X[i] + Bonus[j];

        dp[i][0] = 0;

        for(int j = 0; j < i; j++) dp[i][0] = max(dp[i][0], dp[i - 1][j]);
    }

    ll ans = 0;
	for (int i = 0; i <= N; i++) ans = max(ans, dp[N][i]);
	cout << ans << endl;

    return 0;
} 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0