답안 #562483

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
562483 2022-05-14T13:04:40 Z aryan12 Lamps (JOI19_lamps) C++17
4 / 100
222 ms 36028 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;
	int cnt = 0, cnt1 = 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;
			ans = min(ans, ans - cnt + cnt1 / 2 + 1);
			cnt = 0;
			cnt1 = 0;
		}
		else
		{
			ans++;
			// if(istoggleon) ans += values[i][1];
			// else 		  ans += values[i][0];
		}
		if(!istoggleon)
		{
			cnt += values[i][0];
			cnt1++;
		}
	}
	if(!istoggleon) ans = min(ans, ans - cnt + cnt1 / 2 + 2); // if we toggle the last steps
	else ans = min(ans, ans - cnt + cnt1 / 2 + 1);
	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:89: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]
   89 |  for(int i = 0; i < values.size(); i++)
      |                 ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Incorrect 1 ms 212 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Incorrect 1 ms 212 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 21 ms 19140 KB Output is correct
8 Correct 164 ms 35992 KB Output is correct
9 Correct 161 ms 35960 KB Output is correct
10 Correct 222 ms 36028 KB Output is correct
11 Correct 159 ms 36008 KB Output is correct
12 Correct 20 ms 19148 KB Output is correct
13 Correct 25 ms 19156 KB Output is correct
14 Correct 29 ms 19980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Incorrect 1 ms 212 KB Output isn't correct
14 Halted 0 ms 0 KB -