Solution
2024-08-17 10:30:44
发布于:广东
0阅读
0回复
0点赞
#include <iostream>
using namespace std;
int N, A[1000001];
static int find_smallest_01_gap(void)
{
int smallest_gap = 2000000, current_start = -1;
for (int i = 0; i <= 1000000; i++)
if (A[i] != 0) {
if (current_start != -1 && A[current_start] != A[i] && i - current_start < smallest_gap)
smallest_gap = i - current_start;
current_start = i;
}
return smallest_gap;
}
static int num_sick_clusters(void)
{
int last_state = 0, answer = 0;
for (int i = 0; i <= 1000000; i++)
if (A[i] != 0) {
if (A[i] != last_state && A[i] == 1) answer++;
last_state = A[i];
}
return answer;
}
static int num_sick_gaps(int r)
{
int answer = 0, current_start = 0;
for (int i = 0; i <= 1000000; i++)
if (A[i] != 0) {
if (current_start != 0 && A[current_start] == 1 && A[i] == 1 && i - current_start >= r)
answer++;
current_start = i;
}
return answer;
}
int main()
{
int x, s;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> x >> s;
if (s == 1) { A[x] = 1; }
if (s == 0) { A[x] = -1; }
}
int r = find_smallest_01_gap();
cout << num_sick_clusters() + num_sick_gaps(r) << "\n";
return 0;
}
这里空空如也
有帮助,赞一个