Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
using namespace std; const int maxn = 30005; const int INF = 2e9; int n, a[maxn]; int dp[maxn][4]; int rec(int at, int medal) { if(at == n) { return 0; } if(dp[at][medal] != -1) { return dp[at][medal]; } int res = INF; if(medal == 1) { for(int new_medal = 1; new_medal <= 3; new_medal++) { int promena = 0; if(medal != a[at]) { promena = 1; } res = min(res, rec(at + 1, new_medal) + promena); } } else if(medal == 2) { for(int new_medal = 2; new_medal <= 3; new_medal++) { int promena = 0; if(medal != a[at]) { promena = 1; } res = min(res, rec(at + 1, new_medal) + promena); } } else { int promena = 0; if(medal != a[at]) { promena = 1; } res = min(res, rec(at + 1, 3) + promena); } dp[at][medal] = res; return res; } int main() { cin >> n; for(int i = 0; i < n; i++) { cin >> a[i]; } memset(dp, -1, sizeof dp); int res = INF; for(int i = 1; i <= 3; i++) { res = min(res, rec(0, i)); } reverse(a, a + n); memset(dp, -1, sizeof dp); for(int i = 1; i <= 3; i++) { res = min(res, rec(0, i)); } cout << res << endl; return 0; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)