Spaceless

Codeforces Round

小号 RANK 291

1584 → 1706

A - Average Height

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<bits/stdc++.h>
//#define int long long//__int128
#define mmst0(x) memset(x,0,sizeof(x))
#define mmst3f(x) memset(x,0x3f,sizeof(x))
#define pb(x) emplace_back(x)
#define mkp(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double rld;
typedef unsigned long long ull;

const rld eps = 1e-6;
const int INF=0x3f3f3f3f;//0x3f3f3f3f3f3f3f3f;//LLINF

int read(){int s=0,w=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48);ch=getchar();} return s*w;}
//void prt(int x){if(x<0){putchar('-');x=-x;}if(x>9)prt(x/10);putchar((char)(x%10+'0'));}

int n;
int a[10000];

void work()
{
n=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=n;i++) if(a[i]%2)printf("%d ",a[i]);
for(int i=1;i<=n;i++) if(a[i]%2==0)printf("%d ",a[i]);
printf("\n");
return;
}

signed main()
{
//ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); //freopen(".in", "r", stdin);//freopen(".out", "w", stdout);
signed T=read();
for(signed Case=1; Case<=T; Case++)
{
//printf("Case %d: ",Case);
work();
}
return 0;
}

B - TMT Document

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
//#define int long long//__int128
#define mmst0(x) memset(x,0,sizeof(x))
#define mmst3f(x) memset(x,0x3f,sizeof(x))
#define pb(x) emplace_back(x)
#define mkp(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double rld;
typedef unsigned long long ull;

const rld eps = 1e-6;
const int INF=0x3f3f3f3f;//0x3f3f3f3f3f3f3f3f;//LLINF

int read(){int s=0,w=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48);ch=getchar();} return s*w;}
//void prt(int x){if(x<0){putchar('-');x=-x;}if(x>9)prt(x/10);putchar((char)(x%10+'0'));}

int n;
string s;

void work()
{
cin>>n>>s;int mx=0;
stack<char> st;
for(int i=0;i<(int)s.size();i++)
{
if(s[i]=='T')
{
st.push('T');
}
if(s[i]=='M' )
{
mx++;
if(!st.empty())
{
st.pop();
}
else
{
cout<<"NO"<<endl;
return;
}
}
}
while(mx--)st.pop();
if(!st.empty())
{
cout<<"NO"<<endl;
return;
}
for(int i=(int)s.size()-1;i>=0;i--)
{
if(s[i]=='T')
{
st.push('T');
}
if(s[i]=='M' )
{
mx++;
if(!st.empty())
{
st.pop();
}
else
{
cout<<"NO"<<endl;
return;
}
}
}
cout<<"YES"<<endl;
return;
}

signed main()
{
//ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); //freopen(".in", "r", stdin);//freopen(".out", "w", stdout);
signed T;cin>>T;
for(signed Case=1; Case<=T; Case++)
{
//printf("Case %d: ",Case);
work();
}
return 0;
}

C - The Sports Festival

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
#define int long long//__int128
#define mmst0(x) memset(x,0,sizeof(x))
#define mmst3f(x) memset(x,0x3f,sizeof(x))
#define pb(x) emplace_back(x)
#define mkp(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double rld;
typedef unsigned long long ull;

const rld eps = 1e-6;
const int INF=0x3f3f3f3f;//0x3f3f3f3f3f3f3f3f;//LLINF

int read(){int s=0,w=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48);ch=getchar();} return s*w;}
//void prt(int x){if(x<0){putchar('-');x=-x;}if(x>9)prt(x/10);putchar((char)(x%10+'0'));}

void work()
{
int n=read();
vector<int> a(n);
for (int i=0;i<n;i++)a[i]=read();
sort(a.begin(), a.end());
vector<vector<int> > dp(n, vector<int>(n));
for (int d=1;d<n;d++)
for (int i=0;i+d<n;i++)
dp[i][i+d]=min(dp[i][i+d-1],dp[i+1][i+d])+a[i+d]-a[i];
cout<<dp[0][n-1]<<endl;
return;
}

signed main()
{
//ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); //freopen(".in", "r", stdin);//freopen(".out", "w", stdout);
signed T=1;
for(signed Case=1; Case<=T; Case++)
{
//printf("Case %d: ",Case);
work();
}
return 0;
}

D - Binary Literature

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
int read(){int s=0,w=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48);ch=getchar();} return s*w;}
int n;
int a[3][501009];

string buildx(int x,int y,int cxx)
{
string tmp1=""; int ptr=1;
for(int i = 1; i <= 2 * n; i++)
if(a[y][i] == cxx)
{
while(ptr <= 2 * n && a[x][ptr] != a[y][i]) tmp1 += a[x][ptr++] ? "1" : "0";
tmp1 += a[y][i] ? "1" : "0";
ptr++;
}
else tmp1 += a[y][i] ? "1" : "0";
while(ptr <= 2 * n) tmp1 += a[x][ptr++] ? "1" : "0";
return tmp1;
}
string build(int x, int y)
{
string tmp1 =buildx(x,y,0),tmp2 =buildx(x,y,1);
if(tmp1.size() < tmp2.size()) return tmp1;
else return tmp2;
}
void work()
{
n = read();
for(int j = 0; j < 3; j++)
for(int i = 1; i <= 2 * n; i++)
scanf("%1d", &a[j][i]);
string t1 = build(0, 1),t2 = build(0, 2),t3 = build(1, 2);
if(t3.size() < t2.size()) swap(t3, t2);
if(t2.size() < t1.size()) swap(t2, t1);
cout << t1 << endl;
}
signed main()
{
int Case = read();
while(Case--) work();
return 0;
}

E - Almost Sorted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
#define int long long
#define mmst0(x) memset(x,0,sizeof(x))
#define mmst3f(x) memset(x,0x3f,sizeof(x))
#define pb(x) emplace_back(x)
#define mkp(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double rld;
typedef unsigned long long ull;

const rld eps = 1e-6;
const int INF=0x3f3f3f3f;//0x3f3f3f3f3f3f3f3f;//LLINF

int read(){int s=0,w=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48);ch=getchar();} return s*w;}

int n,k;
int a[100003];

void work()
{
n=read();k=read()-1;
if (n <= 61 && (int)1<<(n-1) <=k)
{
printf("-1\n");
return;
}
iota(a+1, a+n+1, 1);
for (int i=60,j;i>=0;i=j)
{
j = i-1;
if (~(k>>i)&((int)1)) continue;
while (j >= 0 && ((k>>j)&1))j--;
reverse(a+n-1-i, a+n-j);
}
for (int i=1;i<=n;i++) printf("%lld%c",a[i]," \n"[i==n]);
return;
}

signed main()
{
int T=read();
while(T--)work();
return 0;
}