【解题报告】CF DIV2 #ROUND 715 A~D

​比赛链接​​​ rating,已经无所谓了hh
B想了个假算法,卡了半天,C也没时间看,我蠢爆了。

A.Average Height

思路
速A了,先把奇数全部输出,然后把偶数全部输出即可
代码

// Problem: A. Average Height
// Contest: Codeforces - Codeforces Round #715 (Div. 2)
// URL: https://codeforces.com/contest/1509/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
#define MULINPUT
/*DATA & KEY
t 1 500
n total 2 2000
ai 1 2e5
*/
int T;
const int N=2E3+10;
int odd[N],even[N];
void solve(int C)
{
//NEW DATA CLEAN
//NOTE!!!
int a=0,b=0;
int n;cin>>n;
for(int i=1,x;i<=n;i++)
{
cin>>x;
if(x&1)odd[++a]=x;
else even[++b]=x;
}
for(int i=1;i<=a;i++)cout<<odd[i]<<" ";
for(int i=1;i<=b;i++)cout<<even[i]<<" ";
cout<<endl;


}

int main()
{
#ifdef MULINPUT
scanf("%d",&T);
for(int i=1;i<=T;i++)solve(i);
#else
solve(1);
#endif
return 0;
}

B. TMT Document

思路
写了半天假算法,疯狂爆WA,后面人工打表纠错才整出来。写完后发现自己是个憨憨。
假算法
T分别从开头和结尾匹配,碰到M就匹配,反例

TMTTMT

正解
其实类似括号匹配,对于每个M,要保证其前缀T足够,后缀T的数量也足够,然后正好2*M=T。
实现的话,正序逆序遍历一遍记录T前缀即可
代码

// Problem: B. TMT Document
// Contest: Codeforces - Codeforces Round #715 (Div. 2)
// URL: https://codeforces.com/contest/1509/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
#define MULINPUT
/*DATA & KEY
t 1 5e3
n total 3 1e5

*/
int T;
const int N=1E5+10;
char s[N];

void solve(int C)
{
//NEW DATA CLEAN
memset(s,0,sizeof s);
//NOTE!!!
int n;cin>>n;
cin>>s+1;
// cout<<s<<endl;
if(n%3!=0){puts("NO");return;}
int a=0,b=0,cnt=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='T')
{
a++;
cnt++;
}
else if(s[i]=='M')
{
a--;
if(a<0){puts("NO");return;}
}
}
for(int i=n;i>=1;i--)
{
if(s[i]=='T')
{
b++;
}
else if(s[i]=='M')
{
b--;
if(b<0){puts("NO");return;}
}
}
if(cnt==2*(n-cnt))puts("YES");
else puts("NO");
}

int main()
{
#ifdef MULINPUT
scanf("%d",&T);
for(int i=1;i<=T;i++)solve(i);
#else
solve(1);
#endif
return 0;
}

C. The Sports Festival

思路

【解题报告】CF DIV2 #ROUND 715 A~D_i++

赛后想了个贪心结果样例都没过

【解题报告】CF DIV2 #ROUND 715 A~D_c++_02


正解貌似区间DP,​​参考这位大佬的博客​​以及​​这位的博客​​ 最大值和最小值在两边的时候答案不会增加

这样我们可用先排序,然后对区间进行扩展。排序后每次扩展的贡献为a[r]-a[l]

代码

// Problem: C. The Sports Festival
// Contest: Codeforces - Codeforces Round #715 (Div. 2)
// URL: https://codeforces.com/contest/1509/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
//#define MULINPUT
/*DATA & KEY

*/
int T;
const int N=2010;
LL a[N],dp[N][N];
void solve(int C)
{
//NEW DATA CLEAN

//NOTE!!!
int n;cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n);

for(int len=2;len<=n;len++)
for(int l=0;l+len-1<n;l++)
{
int r=l+len-1;
dp[l][r]=min(dp[l][r-1],dp[l+1][r])+a[r]-a[l];
}
cout<<dp[0][n-1]<<endl;

}

int main()
{
#ifdef MULINPUT
scanf("%d",&T);
for(int i=1;i<=T;i++)solve(i);
#else
solve(1);
#endif
return 0;
}

D. Binary Literature

思路
题意:给3个长度为2n的01串,要构造出至少包含其中两个串的长度<=3n的01串(可用非连续)
3个串,3个指针。因为只有01,所以必有两个串一样。如果一样那么输出一个答案并且这两个串的指针后移,直到一个指针到2n。然后输出剩余长度最小的串即可。
代码

// Problem: D. Binary Literature
// Contest: Codeforces - Codeforces Round #715 (Div. 2)
// URL: https://codeforces.com/contest/1509/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// FishingRod

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
#define MULINPUT
/*DATA & KEY

*/
int T;
void solve(int C)
{
//NEW DATA CLEAN

//NOTE!!!
int n;cin>>n;
string a,b,c;cin>>a>>b>>c;
int pa=0,pb=0,pc=0;
while(pa<2*n&&pb<2*n&&pc<2*n)
{
if(a[pa]==b[pb])
{
cout<<a[pa];
pa++;pb++;
}
else if(a[pa]==c[pc])
{
cout<<a[pa];
pa++;pc++;
}
else if(b[pb]==c[pc])
{
cout<<b[pb];
pb++;pc++;
}
}
if(pa==2*n)
{
if(pb<pc)cout<<c.substr(pc);
else cout<<b.substr(pb);
}
else if(pb==2*n)
{
if(pc<pa)cout<<a.substr(pa);
else cout<<c.substr(pc);
}
else
{
if(pb<pa)cout<<a.substr(pa);
else cout<<b.substr(pb);
}
puts("");

}

int main()
{
#ifdef MULINPUT
scanf("%d",&T);
for(int i=1;i<=T;i++)solve(i);
#else
solve(1);
#endif
return 0;
}

反思

A:

没啥好说的,提升读题速度

B:

字符串子序列,对于“ABA","()"这种对称的子串(可不连续),可用采取前后缀需求匹配的方式来弄
数量正好+前缀满足需求+后缀满足需求
对于后缀,可用直接反向遍历,是否满足需求那就直接定义一个变量看是否>=0

C:

区间DP可用于自己选要加入的数,使得一个区间的值最优。

D:

构造包含指定子串(不连续)的原串,可用思考使用多个指针