답안 #150653

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
150653 2019-09-01T08:47:00 Z HSNU(#3687, joylintp, 147, wiwiho) HicCup (FXCUP4_hiccup) C++17
24 / 100
65 ms 4352 KB
#include<bits/stdc++.h>
#include "hiccup.h"

using namespace std;

bool chk(string s)
{
    if (s[0] == '!')
        return false;
    for (int i = 1; i < (int)s.size(); i++)
        if (s[i] == '!' && s[i - 1] == 'H')
            return false;

    stack<char> stk;
    for (int i = 0; i < (int)s.size(); i++)
        if (s[i] == '!')
            continue;
        else if (stk.empty())
            stk.push(s[i]);
        else if (stk.top() == 'H' && s[i] == 'C')
            stk.pop();
        else
            stk.push(s[i]);

    return stk.empty();
}

int HicCup(string s)
{
	long long N = s.size();

	if (!chk(s))
        return -1;

    long long ans = 1e9;

    stack<long long> stk;
    for (int i = 0; i < (int)s.size(); i++)
    {
        if (s[i] == 'H')
            stk.push(-1);
        else
        {
            long long ttl = 0, cnt = 0;
            vector<long long> quu;
            while (stk.top() != -1)
                quu.push_back(stk.top()), ttl += stk.top(), cnt++, stk.pop();
            stk.pop();

            if (cnt)
            {
                long long avg = ttl / cnt;
                for (int i = 0; i < cnt - 1; i++)
                    if (quu[i] < avg)
                        ans = min(ans, quu[i]);
                    else
                        quu[i + 1] += quu[i] - avg, quu[i] = avg;
                ans = min(ans, min(quu[cnt - 1], avg));
            }

            i++;
            cnt = 0;
            while (s[i] == '!' && i < (int)s.size())
                cnt++, i++;
            i--;
            stk.push(cnt);
        }
    }

    long long ttl = 0, cnt = 0;
    vector<long long> quu;
    while (!stk.empty())
        quu.push_back(stk.top()), ttl += stk.top(), cnt++, stk.pop();

    if (cnt)
    {
        long long avg = ttl / cnt;
        for (int i = 0; i < cnt - 1; i++)
            if (quu[i] < avg)
                ans = min(ans, quu[i]);
            else
                quu[i + 1] += quu[i] - avg, quu[i] = avg;
        ans = min(ans, min(quu[cnt - 1], avg));
    }

	return (int)ans;
}

Compilation message

hiccup.cpp: In function 'int HicCup(std::__cxx11::string)':
hiccup.cpp:30:12: warning: unused variable 'N' [-Wunused-variable]
  long long N = s.size();
            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 8 ms 640 KB Output is correct
5 Correct 62 ms 4224 KB Output is correct
6 Correct 24 ms 4352 KB Output is correct
7 Correct 23 ms 4352 KB Output is correct
8 Correct 65 ms 4352 KB Output is correct
9 Correct 61 ms 4220 KB Output is correct
10 Correct 22 ms 4224 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 8 ms 640 KB Output is correct
5 Correct 62 ms 4224 KB Output is correct
6 Correct 24 ms 4352 KB Output is correct
7 Correct 23 ms 4352 KB Output is correct
8 Correct 65 ms 4352 KB Output is correct
9 Correct 61 ms 4220 KB Output is correct
10 Correct 22 ms 4224 KB Output is correct
11 Correct 20 ms 4224 KB Output is correct
12 Correct 17 ms 4352 KB Output is correct
13 Correct 14 ms 4224 KB Output is correct
14 Correct 5 ms 128 KB Output is correct
15 Correct 14 ms 4096 KB Output is correct
16 Correct 6 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 11 ms 640 KB Output is correct
19 Incorrect 19 ms 4224 KB Output isn't correct
20 Halted 0 ms 0 KB -