A - Rightmost
#include<bits/stdc++.h>
using namespace std;
int32_t main() {
string s;
cin >> s;
for( int i = s.size() ; i >= 1 ; i -- )
if( s[i-1] == 'a' ) cout << i , exit(0);
cout << "-1";
return 0;
}
B - Adjacency List
#include<bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
const int N = 1e5+5;
set<int> e[N];
int32_t main() {
int n = read() , m = read();
for( int u , v ; m ; m -- )
u = read() , v = read() , e[u].insert(v) , e[v].insert(u);
for( int i = 1 ; i <= n ; i ++ ){
cout << e[i].size() << " ";
for( auto it : e[i] )
cout << it << " ";
cout << "\n";
}
return 0;
}
C - Previous Permutation
直接用prev_permutation
#include<bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
const int N = 1e5+5;
int32_t main() {
int n = read();
vector<int> ve;
for( int x ; n ; n -- )
x = read() , ve.push_back(x);
prev_permutation( ve.begin() , ve.end() );
for( auto it : ve )
cout << it << " ";
}
D - Divide by 2 or 3
每一个数一定可以表示为\(a_i+2b_i+3c_i\)这样的话我们只要判断一下整个序列的\(a_i\)是否相等就可以判断是否可以通过操作是的序列的值变的相等。
然后我们在找出\(b_i,c_i\)的最小值就可以计算出最小操作次数
#include<bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
const int N = 1005;
int a[N] , b[N] , c[N];
int32_t main() {
int n = read();
for( int i = 1 ; i <= n ; i ++ ) {
a[i] = read();
while( a[i] % 2 == 0 ) a[i] /= 2 , b[i] ++;
while( a[i] % 3 == 0 ) a[i] /= 3 , c[i] ++;
}
int minb = INT_MAX , minc = INT_MAX;
for( int i = 1 ; i <= n ; i ++ ){
if( a[i] != a[1] )
cout << "-1\n" , exit(0);
minb = min( minb , b[i] ) , minc = min( minc , c[i] );
}
int res = 0;
for( int i = 1 ; i <= n ; i ++ )
res += b[i] - minb + c[i] - minc;
cout << res << "\n";
}
E - Round Trip
把所有的相邻的道路用并查集合并,然后判断起点周围是否存在一组点是一个集合中的
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n , m;
cin >> n >> m;
vector g( n+1 , vector<char>( m+1 ) );
for( int i = 1 ; i <= n ; i ++ )
for( int j = 1 ; j <= m ; j ++ )
cin >> g[i][j];
vector<int> fa(n*m+1);
for( int i = 1 ; i <= n*m ; i ++ ) fa[i] = i;
function<int(int)> getfa = [&]( int x ){
if( fa[x] == x ) return x;
return fa[x] = getfa(fa[x]);
};
auto merge = [&]( int x , int y ){
x = getfa(x) , y = getfa(y);
fa[x] = y;
};
auto id = [m](int x , int y ){
return (x-1)*m+y;
};
for( int i = 1 ; i < n ; i ++ )
for( int j = 1 ; j <= m ; j ++ )
if( g[i][j] == '.' && g[i+1][j] == '.' )
merge( id(i,j) , id(i+1,j) );
for( int i = 1 ; i <= n ; i ++ )
for( int j = 1 ; j < m ; j ++ )
if( g[i][j] == '.' && g[i][j+1] == '.' )
merge( id(i,j) , id(i,j+1) );
int sx = 0 , sy = 0;
for( int i = 1 ; i <= n && sx == sy && sx == 0 ; i ++)
for( int j = 1 ; j <= m && sx == sy && sx == 0 ; j ++ )
if( g[i][j] == 'S' ) sx = i , sy = j;
vector<pair<int,int>> d;
if( sx+1 >= 1 && sx+1 <= n && sy >= 1 && sy <= m ) d.push_back( {sx+1,sy} );
if( sx-1 >= 1 && sx-1 <= n && sy >= 1 && sy <= m ) d.push_back( {sx-1,sy} );
if( sx >= 1 && sx <= n && sy+1 >= 1 && sy+1 <= m ) d.push_back( {sx,sy+1} );
if( sx >= 1 && sx <= n && sy-1 >= 1 && sy-1 <= m ) d.push_back( {sx,sy-1} );
for( auto [ax,ay] : d )
for( auto [bx,by] : d){
if( ax == bx && ay == by ) continue;
if( getfa( id(ax,ay) ) == getfa( id(bx,by) ) ) cout << "Yes\n" , exit(0);
}
cout << "No\n";
return 0;
}
标签:AtCoder,ch,&&,Beginner,int,read,while,276,getchar
From: https://www.cnblogs.com/PHarr/p/16867627.html