LoginSignup
0
0

More than 1 year has passed since last update.

AtCoder Beginner Contest 263

Posted at

A - Full House

シミュレートします。

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() {
    vector<int> A(5);
    rep(i, 5) cin >> A[i];

    sort(A.begin(), A.end());


    if((A[0] == A[2] && A[3] == A[4]) || (A[0] == A[1] && A[2] == A[4])){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;      
    }

    return 0;
} 

B - Ancestor

dfsかループでシミュレートします。

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> P(N+1);
    repx(i, 2,  N+1) cin >> P[i];

    int cnt=0;
    while(1){
        N = P[N];
        cnt++;
        if(N==1) break;
    }
    cout << cnt << endl;

    return 0;
} 

C - Monotonically Increasing

dfsを使うと綺麗に実装できます。

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 N, M;
set<vector<int>> st;

void dfs(vector<int> v){
    if(v.size() == N){
        st.insert(v);
        return;
    }

    int i = 1;
    if(v.size()) i = v[v.size()-1] + 1;

    for(; i<=M; i++){
        v.push_back(i);
        dfs(v);
        v.pop_back();
    }
}

int main() {
    cin >> N >> M;

    dfs(vector<int>());

    for(vector<int> s:st){
        for(auto v:s){
            cout << v << ' ';
        }
        cout << 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