CSES - HIIT Open 2016 - Results
Submission details
Task:HIIT remains
Sender:LTR
Submission time:2016-05-28 13:36:19 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.06 sdetails
#3ACCEPTED0.09 sdetails
#4ACCEPTED0.06 sdetails

Code

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <set>

int main()
{
	int t;
	std::cin >> t;
	for (int ti = 0; ti < t; ++ti) {
		std::string s;
		std::cin >> s;
		std::vector<int> H;
		std::vector<int> I;
		std::vector<int> T;
		for (int i = 0; i < int(s.size()); ++i) {
			auto c = s[i];
			if (c == 'H') H.push_back(i);
			if (c == 'I') I.push_back(i);
			if (c == 'T') T.push_back(i);
		}

		if (H.size() == 0 || T.size() == 0) {
			std::cout << 0 << std::endl;
			continue;
		}

		int Hmin = H[0];
		int Tmax = T[T.size() - 1];
		std::vector<int> Iok;
		for (auto i : I) {
			if (i < Hmin) continue;
			if (i > Tmax) break;
			Iok.push_back(i);
		}

		if (Iok.size() < 2) {
			std::cout << 0 << std::endl;
			continue;
		}

		std::vector<long long> Hbefore(Iok.size(), 0);
		std::vector<long long> Tafter(Iok.size(), 0);

		int Hi = 0; // num of H values before this I
		int Ti = 0; // num of T values before this I
		for (int iI = 0; iI < int(Iok.size()); ++iI) {
			int i = Iok[iI];
			while (Hi < int(H.size()) && H[Hi] < i) Hi++;
			Hbefore[iI] = Hi;

			while (Ti < int(T.size()) && T[Ti] < i) Ti++;
			Tafter[iI] = T.size() - Ti;
		}

		std::vector<long long> TafterCumu(Iok.size());
		TafterCumu[Iok.size() - 1] = Tafter[Iok.size() - 1];
		for (int iI = int(Iok.size()) - 2; iI >= 0; --iI) {
			TafterCumu[iI] = Tafter[iI] + TafterCumu[iI+1];
		}

		// std::cerr << "Hbefore:";
		// for (auto h : Hbefore) std::cerr << " " << h;
		// std::cerr << std::endl;
		// std::cerr << "Iok:";
		// for (auto h : Iok) std::cerr << " " << h;
		// std::cerr << std::endl;
		// std::cerr << "Tafter:";
		// for (auto h : Tafter) std::cerr << " " << h;
		// std::cerr << std::endl;
		// std::cerr << "TafterCumu:";
		// for (auto h : TafterCumu) std::cerr << " " << h;
		// std::cerr << std::endl;

		long long count = 0;
		for (int iI = 0; iI < (int(Iok.size()) - 1); ++iI) {
			count += Hbefore[iI] * TafterCumu[iI + 1];
		}

		std::cout << count << std::endl;
	}
}

Test details

Test 1

Verdict: ACCEPTED

input
100
IIITIIIHITHTHIIITIII
HIHIIIIIIHIIITHIIIII
ITTIIIITIIIIIIITIIIT
IITHITITIHHIITTTIIII
...

correct output
12
84
0
37
96
...

user output
12
84
0
37
96
...

Test 2

Verdict: ACCEPTED

input
100
TIIHHITTITITIHTHIIIITHIHHIIHTI...

correct output
606723862
621369559
655243897
550750615
717769300
...

user output
606723862
621369559
655243897
550750615
717769300
...

Test 3

Verdict: ACCEPTED

input
10
TTHTHHTIIIIIITHIIHIITITTITTIIH...

correct output
64668032062669502
66159978956790306
65755072918424640
64408596558953628
65238005187079543
...

user output
64668032062669502
66159978956790306
65755072918424640
64408596558953628
65238005187079543
...

Test 4

Verdict: ACCEPTED

input
3
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH...

correct output
781234375000000000
4999750003
0

user output
781234375000000000
4999750003
0