# Codeforces Round #605 (Div. 3)

A. Three Friends

#include <bits/stdc++.h>
using namespace std;

int q, a[3];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> q;
while( q-- ){
for( int i=0; i<3; i++ ) cin >> a[i];
sort(a, a+3);
if( a[2]-a[0]<=2 ) cout << "0" << endl;
else cout << (a[2]-a[0]-2)*2 << endl;
}

return 0;
}
A.Three Friends

B.Snow Walking Robot

#include <bits/stdc++.h>
using namespace std;

int q, cl, cr, cu, cd;
string s;
int main(){
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> q;
while( q-- ){
cin >> s;
cl = cr = cu = cd = 0;
for( char i:s ){
cl += i=='L';
cr += i=='R';
cu += i=='U';
cd += i=='D';
}
cu = min(cu, cd);
cl = min(cl, cr);
if( cu==0 || cl==0 ){
cl = min(1, cl);
cu = min(1, cu);
}
string ans;
for( int i=0; i<cl; i++ ) ans += 'R';
for( int i=0; i<cu; i++ ) ans += 'U';
for( int i=0; i<cl; i++ ) ans += 'L';
for( int i=0; i<cu; i++ ) ans += 'D';
cout << ans.size() << endl << ans << endl;
}

return 0;
}
B.Snow Walking Robot

C.Yet Another Broken Keyboard

set存一下这k个字母，对s中每一段连续的可以在set中查找到的分别进行统计汇总即可，每一段长度len_i对答案的贡献为(len_i)*(len_i+1)/2.

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int n, k;
string s;
set<char> a;
ll ans, tmp;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k >> s;
for( int i=0; i<k; i++ ){
char ch;
cin >> ch;
a.insert(ch);
}
int len = s.size();
for( int i=0; i<len; i++ ){
if( a.find(s[i])!=a.end() )
tmp ++;
else{
ans += tmp*(tmp+1)/2;
tmp = 0;
}
}
if(tmp) ans += tmp*(tmp+1)/2;
cout << ans << endl;

return 0;
}
C.Yet Another Broken Keyboard

D. Remove One Element

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
int a[maxn], n, l[maxn], r[maxn], ans;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for( int i=0; i<n; i++ ) cin >> a[i];
int tmp = 1;
for( int i=1; i<n; i++ ){
if( a[i]>a[i-1] ) tmp++;
else tmp = 1;
ans = max(ans, tmp);
}
l[0] = 1;
for( int i=1; i<n; i++ ){
if( a[i]>a[i-1] ) l[i] = l[i-1]+1;
else l[i] = 1;
}
r[n-1] = 1;
for( int i=n-2; ~i; i-- ){
if( a[i]<a[i+1] ) r[i] = r[i+1]+1;
else r[i] = 1;
}
for( int i=1; i<n-1; i++ )
if( a[i-1]<a[i+1] ) ans = max(ans, l[i-1]+r[i+1]);
cout << ans << endl;

return 0;
}
D. Remove One Element

E.Nearest Opposite Parity

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int n, a[maxn], ans[maxn];
vector<int> p[maxn];
int main(){
memset( ans, -1, sizeof(ans) );
scanf("%d", &n);
for( int i=0; i<n; i++ ) scanf("%d", &a[i]);
queue<int> q;
for( int i=0; i<n; i++ ){
if( i-a[i]>=0 ){
p[i-a[i]].push_back(i);
if( a[i-a[i]]%2 != a[i]%2 ) ans[i] = 1;
}
if( i+a[i]<n ){
p[i+a[i]].push_back(i);
if( a[i+a[i]]%2 != a[i]%2 ) ans[i] = 1;
}
if( ans[i]==1 ) q.push(i);
}
while( q.size() ){
int x = q.front();
q.pop();
for( auto i:p[x] ){
if( ans[i]==-1 && a[i]%2==a[x]%2 ){
ans[i] = ans[x]+1;
q.push(i);
}
}
}
for( int i=0; i<n; i++ ){
printf("%d", ans[i]);
if( i==n-1 ) puts("");
else putchar(' ');
}

return 0;
}
E.Nearest Opposite Parity
posted @ 2019-12-15 17:11  CoffeeCati  阅读(152)  评论(0编辑  收藏  举报