본문 바로가기

USACO/Silver

백준 14453 Hoof, Paper, Scissors (USACO January 2017 Silver 2번)

문제 링크: https://www.acmicpc.net/problem/14453

 

14453번: Hoof, Paper, Scissors (Silver)

You have probably heard of the game "Rock, Paper, Scissors". The cows like to play a similar game they call "Hoof, Paper, Scissors". The rules of "Hoof, Paper, Scissors" are simple. Two cows play against each-other. They both count to three and then each s

www.acmicpc.net

Prefix Sum

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
#include <bits/stdc++.h>
using namespace std;
 
int n, a[100001][3];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> n;
    for(int i = 1; i <= n; i++) {
        char c;
        cin >> c;
        for(int j = 0; j < 3; j++) {
            a[i][j] = a[i - 1][j];
        }
        if(c == 'P') a[i][0= a[i - 1][0+ 1;
        else if(c == 'H') a[i][1= a[i - 1][1+ 1;
        else a[i][2= a[i - 1][2+ 1;
    }
 
    int ans = 0;
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            for(int k = 1; k <= n; k++) {
                ans = max(ans, a[k][i] + a[n][j] - a[k][j]);
            }
        }
    }
 
    cout << ans << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter