A. Repeating Cipher
#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
cin >> n >> s;
int idx = 0,cnt = 1;
while(idx < n){
cout << s[idx];
idx += cnt;
cnt ++;
}
cout << endl;
return 0;
}
B. Array Stabilization
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n,a[N];
void solve(){
sort(a + 1,a + 1 + n);
printf("%d\n", min(a[n - 1] - a[1], a[n] - a[2]));
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; ++ i){
scanf("%d", &a[i]);
}
solve();
return 0;
}
C. Powers Of Two
-
解题思路 将
n
n
n按二进制位展开,那么展开后的每个二进制数都是
2
2
2的次幂。那么我们又知道对于
2
k
,
k
>
0
2^k,k >0
2k,k>0,其可以转换成
2
2
2个
2
k
?
1
2^{k-1}
2k?1,则会使个数加
1
1
1。 根据此原理,我们从高往低凑成
k
k
k个
2
2
2的次幂即可。 -
参考代码
#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[31];
void solve(){
a[0] = 1;
for(int i = 1; i < 31; ++ i){
a[i] = a[i - 1] * 2;
}
priority_queue<int> pos;
for(int i = 0; i < 31; ++ i){
if(n & a[i]){
pos.push(i);
}
}
while(pos.size() < k && pos.top() > 0){
int idx = pos.top();
pos.pop();
pos.push(idx - 1);
pos.push(idx - 1);
}
if(pos.size() == k){
cout << "YES" << endl;
while(!pos.empty()){
cout << a[(int)pos.top()] << " ";
pos.pop();
}
cout << endl;
}
else{
cout << "NO" << endl;
}
}
int main(){
cin >> n >> k;
solve();
return 0;
}
D. Circular Dance
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 2e5 + 10;
int n;
pair<int,int> a[N];
void solve(){
vector<int> q;
q.push_back(1);
int x = a[1].x, y = a[1].y;
bool flag = true;
if(a[x].x == y || a[x].y == y){
flag = false;
}
if(flag){
swap(x,y);
}
q.push_back(x),q.push_back(y);
int len = q.size();
while(len < n){
int cur = a[x].x;
if(y == cur){
cur = a[x].y;
}
q.push_back(cur);
len ++;
x = y;
y = cur;
}
for(int i = 0; i < len; ++ i){
cout << q[i] << " ";
}
cout << endl;
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; ++ i){
scanf("%d%d", &a[i].x, &a[i].y);
}
solve();
return 0;
}
E. Almost Regular Bracket Sequence
#include<bits/stdc++.h>
using namespace std;
int n;
string str;
void solve(){
stack<int> s;
for(int i = 0; i < n; ++ i){
if(str[i] == '('){
s.push(i);
}
else{
if(!s.empty() && str[s.top()] == '('){
s.pop();
}
else{
s.push(i);
}
}
}
int ans = 0;
if(s.size() == 2){
int idx2 = s.top();
s.pop();
int idx1 = s.top();
if(str[idx1] == ')'){
if(str[idx2] == '('){
ans = 0;
}
else{
for(int i = 0; i <= idx1; ++ i){
if(str[i] == ')'){
ans ++;
}
}
}
}
else{
for(int i = idx2; i < n; ++ i){
if(str[i] == '('){
ans ++;
}
}
}
}
cout << ans << endl;
}
int main(){
cin >> n >> str;
solve();
return 0;
}
F. Make It Connected
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
int n,m;
struct node{
int u;
ll w;
bool operator < (const node &A){
return w < A.w;
}
}nodes[N];
struct edge{
int u,v;
ll w;
bool operator < (const edge &A){
return w < A.w;
}
}edges[N << 2];
int tot,father[N];
int find(int x){
int r = x;
while(r != father[r])r = father[r];
int i = x,j;
while(father[i] != r){
j = father[i];
father[i] = r;
i = j;
}
return r;
}
void solve(){
sort(nodes + 1, nodes + 1 + n);
for(int i = 2; i <= n; ++ i){
edges[++ tot].u = nodes[1].u;
edges[tot].v = nodes[i].u;
edges[tot].w = nodes[i].w + nodes[1].w;
}
sort(edges + 1, edges + 1 + tot);
for(int i = 1; i <= n; ++ i)father[i] = i;
ll ans = 0;
for(int i = 1; i <= tot; ++ i){
int fu = find(edges[i].u), fv = find(edges[i].v);
if(n == 1)break;
if(fu != fv){
father[fu] = fv;
ans += edges[i].w;
n --;
}
}
printf("%lld\n", ans);
}
int main(){
scanf("%d%d", &n ,&m);
for(int i = 1; i <= n; ++ i){
scanf("%lld", &nodes[i].w);
nodes[i].u = i;
}
for(int i = 1; i <= m; ++ i){
tot ++;
scanf("%d%d%lld", &edges[tot].u, &edges[tot].v, &edges[tot].w);
}
solve();
return 0;
}
|