LoginSignup
0
0

AtCoder Beginner Contest 350

Posted at

A - Past ABCs

O(1)
先頭の文字列が"ABC"であるか。
数値が0<N, N<350であるか。
数値が316ならNoである。

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() {
    string s;
    cin >> s;

    string t = s.substr(0, 3);
    int no = stoi(s.substr(3, 3));

    if(t != "ABC" || no == 316){
        cout << "No" << endl;
        return 0;
    }

    if(1 <= no && no <= 349){
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;

    return 0;
} 

B - Dentist Aoki

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, Q;
    cin >> N >> Q;
    vector<int> T(Q);
    rep(i, Q) cin >> T[i];

    vector<int> ans(N+1, 1);
    rep(i, Q){
        int no = T[i];
        if(ans[no] == 1){
            ans[no] = 0;
        }else{
            ans[no] = 1;
        }
    }

    int total = 0;
    repx(i, 1, N+1){
        total += ans[i];
    }
    cout << total << endl;

    return 0;
} 

C - Sort

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<int> A(N+1), B(N+1);
    repx(i, 1, N+1){
        int no;
        cin >> no;
        A[i] = no;
        B[no] = i;
    }

    vector<P> v;
    repx(i, 1, N+1){
        if(B[i] == i) continue;

        int no1 = B[i];
        int no2 = B[A[i]];
        int no3 = A[i];
        v.push_back(P(no2, no1));
        swap(A[no1], A[no2]);
        swap(B[i], B[no3]);
    }

    cout << v.size() << endl;
    rep(i, v.size()){
        cout << v[i].first << ' ' << v[i].second << endl;
    }

    return 0;
} 

C++
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