Consider a list of numbers with two operations:
For example: the result of the insertion of a number 4 to the list [1, 2, 1] is the list [1, 2, 1, 4]. If we delete the number 1 from this list, we get the list [2, 1, 4], but if we delete the number 3 from the list [1, 2, 1, 4], the list stays unchanged.
The list is homogeneous if it contains at least two equal numbers and the list is heterogeneous if it contains at least two different numbers. For example: the list [2, 2] is homogeneous, the list [2, 1, 4] is heterogeneous, the list [1, 2, 1, 4] is both, and the empty list is neither homogeneous nor heterogeneous.
Write a program that handles a number of the operations insert and delete on the empty list and determines list’s homogeneity and heterogeneity after each operation.
The first line of the input file contains an integer number n — the number of operations to handle (1 ≤ n ≤ 100 000).
Following n lines contain one operation description each. The operation description consists of a word “insert” or “delete”, followed by an integer number k — the operation argument (−10^9 ≤ k ≤ 10^9).
For each operation output a line, containing a single word, describing the state of the list after the operation:
Input: 11 insert 1 insert 2 insert 1 insert 4 delete 1 delete 3 delete 2 delete 1 insert 4 delete 4 delete 4Output: neither hetero both both hetero hetero hetero neither homo neither neither
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;const int N = 1e5 + 10;
int a[N], lisan[N], num[N];
string b[N];int main()
{int n;cin >> n; // 输入操作次数for (int i = 1; i <= n; i++){cin >> b[i] >> a[i]; // 输入操作类型和值lisan[i] = a[i]; // 复制一份值用于排序和离散化}sort(lisan + 1, lisan + n + 1); // 对lisan数组进行排序int len = unique(lisan + 1, lisan + n + 1) - lisan; // 去除重复元素并计算新的长度int sum1 = 0, sum2 = 0; // 计数变量for (int i = 1; i <= n; i++){int pos = lower_bound(lisan + 1, lisan + len + 1, a[i]) - lisan; // 使用二分查找找到a[i]在lisan中的位置,得到离散化后的值pos//lower_bound用于返回lisan数组中第一个不大于a[i]的地址,减去首地址就可以得到其偏移量if (b[i] == "insert"){num[pos]++; // 在num数组中增加对应位置的计数if (num[pos] == 1)sum1++; // 如果计数为1,则sum1加1if (num[pos] == 2)sum2++; // 如果计数为2,则sum2加1}if (b[i] == "delete"){num[pos]--; // 在num数组中减少对应位置的计数if (num[pos] == 0)sum1--; // 如果计数为0,则sum1减1if (num[pos] == 1)sum2--; // 如果计数为1,则sum2减1}// 根据sum1和sum2的值输出结果if (sum1 >= 2 && sum2 >= 1)cout << "both" << endl;if (sum1 < 2 && sum2 >= 1)cout << "homo" << endl;if (sum1 >= 2 && sum2 < 1)cout << "hetero" << endl;if (sum1 < 2 && sum2 < 1)cout << "neither" << endl;}return 0;
}
本文发布于:2024-02-02 04:29:23,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681936241360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |