934C - A Twisty Movement - CodeForces Solution


brute force dp implementation *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define mod 998244353
using namespace std;
inline void read(int &x)
{int f=0; x=0;
char c=getchar();
while (c<'0'||c>'9')
{if (c=='-') f=1;
c=getchar();
}
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
if (f) x=-x;
}
int a[2005],n,s1[2005],s2[2005],ans;
int main (){
	int i,j,k;
	read(n);
	for(i=1;i<=n;i++){
		read(a[i]);
		s1[i]=s1[i-1]+(a[i]==1);
		s2[i]=s2[i-1]+(a[i]==2);
	}
	for(i=1;i<=n;i++){
		int sa=0,sb=0;
		for(j=i;j>=1;j--)
			sa=max(sa,s1[j]+s2[i]-s2[j-1]);
		for(j=i;j<=n;j++)
			sb=max(sb,s1[j]-s1[i]+s2[n]-s2[j]);
		ans=max(ans,sa+sb);
	}
	printf("%d",ans);
}/*1697936166.2056048*/


Comments

Submit
0 Comments
More Questions

60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person