답안 #562445

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
562445 2022-05-14T11:37:12 Z aryan12 Lamps (JOI19_lamps) C++17
4 / 100
161 ms 37548 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

int n;
string a, b;

const int N = 1e6 + 5;
int seg[N * 4];

void Build(int l, int r, int pos)
{
	if(l == r)
	{
		seg[pos] = a[l] - '0';
		return;
	}
	int mid = (l + r) >> 1;
	Build(l, mid, pos * 2);
	Build(mid + 1, r, pos * 2 + 1);
	seg[pos] = seg[pos * 2] + seg[pos * 2 + 1];
}

int Query(int l, int r, int pos, int ql, int qr)
{
	if(ql <= l && r <= qr)
	{
		return seg[pos];
	}
	if(ql > r || l > qr)
	{
		return 0;
	}
	int mid = (l + r) >> 1;
	return Query(l, mid, pos * 2, ql, qr) + Query(mid + 1, r, pos * 2 + 1, ql, qr);
}

void Solve() 
{
	cin >> n >> a >> b;
	Build(0, n - 1, 1);
	int prev_value = b[0] - '0', start_pos = 0;
	vector<array<int, 2> > values; // {update, toggle}
	for(int i = 1; i < n; i++)
	{
		if(prev_value != b[i] - '0')
		{
			int ans = Query(0, n - 1, 1, start_pos, i - 1);
			int len = i - 1 - start_pos + 1;
			if((ans == len && prev_value == 1) || (ans == 0 && prev_value == 0)) // no need to "update"
			{
				values.push_back({0, 1}); //0 for set state, 1 (+ 1) for toggle state
			}
			else if((ans == len && prev_value == 0) || (ans == 0 && prev_value == 1)) // no need to toggle
			{
				values.push_back({1, 0});
			}
			else
			{
				values.push_back({1, 1});
			}
			start_pos = i;
			prev_value = b[i] - '0';
		}
	}
	int ans = Query(0, n - 1, 1, start_pos, n - 1);
	int len = n - 1 - start_pos + 1;
	if((ans == len && prev_value == 1) || (ans == 0 && prev_value == 0))
	{
		values.push_back({0, 1});
	}
	else if((ans == len && prev_value == 0) || (ans == 0 && prev_value == 1))
	{
		values.push_back({1, 0});
	}
	else
	{
		values.push_back({1, 1});
	}
	// for(int i = 0; i < values.size(); i++)
	// {
	// 	cout << values[i][0] << " " << values[i][1] << "\n";
	// }
	bool istoggleon = false;
	ans = 0;
	for(int i = 0; i < values.size(); i++)
	{
		if(values[i][0] == 0 && values[i][1] == 1)
		{
			istoggleon = false;
			// ans += values[i][0] = 0
		}
		else if(values[i][0] == 1 && values[i][1] == 0)
		{
			if(!istoggleon) ans++; // for opening toggle
			istoggleon = true;
		}
		else
		{
			ans++;
			// if(istoggleon) ans += values[i][1];
			// else 		  ans += values[i][0];
		}
	}
	cout << ans << "\n";
}

int32_t main() 
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++) 
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}

Compilation message

lamp.cpp: In function 'void Solve()':
lamp.cpp:88:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |  for(int i = 0; i < values.size(); i++)
      |                 ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 23 ms 20704 KB Output is correct
8 Correct 161 ms 37548 KB Output is correct
9 Correct 156 ms 37504 KB Output is correct
10 Correct 154 ms 37508 KB Output is correct
11 Correct 157 ms 37536 KB Output is correct
12 Correct 19 ms 20760 KB Output is correct
13 Correct 21 ms 20700 KB Output is correct
14 Correct 27 ms 21476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -