AtCoder Beginner Contest 276

## [A - Rightmost](https://atcoder.jp/contests/abc276/tasks/abc276_a)

O(N)
forのループを行い、'a'の最大値を探索。

```c++: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;
    int index = 0;
    rep(i, s.size()){
        if(s[i] == 'a'){
            index = max(index, i+1);
        }
    }
    if(index == 0) index = -1;
    cout << index << endl;
    return 0;
} 
```

## [B - Adjacency List](https://atcoder.jp/contests/abc276/tasks/abc276_b)

O(NM)
双方向グラフの問題です。
橋の登録が重複した時の為に、ハッシュを使用します。

```c++: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<set<int>> graph(N+1);
    rep(i, M){
        int a, b;
        cin >> a >> b;
        graph[a].insert(b);
        graph[b].insert(a);
    }

    repx(i, 1, N+1){
        int d = graph[i].size();
        cout << d;
        if(d>0) cout << ' ';
        auto itr = graph[i].begin();
        rep(j, d){
            cout << *itr;
            if(j<d-1) cout << ' ';
            itr++;
        }
        cout << endl;
    }

    return 0;
} 
```

## [C - Previous Permutation](https://atcoder.jp/contests/abc276/tasks/abc276_c)

O(N)
prev_permutationを使用するとK-1の順列を取得できます。

```c++: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; }

ll dfs(ll N){
    if(N<=0) return 0;
    if(N==1) return 1;
    return N * dfs(N-1);
}

void solve(ll N, vector<ll> P){
    prev_permutation(P.begin(), P.end());
    rep(i, N){
        cout << P[i];
        if(i < N - 1) cout << ' ';
    }
    cout << endl;
}

int main() {
    ll N;
    cin >> N;
    vector<ll> P(N);
    rep(i, N) cin >> P[i];
    solve(N, P);
    return 0;
} 
```