货物种类
链接:https://ac.nowcoder.com/acm/contest/4462/H
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
某电商平台有n个仓库,编号从1到n。
当购进某种货物的时候,商家会把货物分散的放在编号相邻的几个仓库中。
我们暂时不考虑售出,你是否能知道,当所有货物购买完毕,存放货物种类最多的仓库编号为多少?
输入描述:
在第一行中给出两个正整数n, m, 1\le n, m \le 10^5n,m,1≤n,m≤10
5
,分别代表仓库的数目和进货的次数。
接下来 m 行,每行三个正整数l,r,d,1 \le l,r \le n, 1 \le d \le 10^9l,r,d,1≤l,r≤n,1≤d≤10
9
。编号在l和r之间的仓库收进编号为d的货物。
输出描述:
在一行中输出存放货物种类最多的仓库编号,若满足条件的仓库不止一个,则输出编号最小的那个。
示例1
输入
5 5
1 1 1
3 3 1
2 5 2
5 5 1
4 5 1
输出
3
代码:
#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
struct node{
ll begin, end, value;
}a[200030];
ll n, m, k, i, j, vb;
bool cmp(node n1, node n2) {
if (n1.value != n2.value)return n1.value < n2.value;
else if (n1.begin != n2.begin)return n1.begin < n2.begin;
else return n1.end < n2.end;
}
int main() {
FAST_IO;
while (cin >> n >> m) {
for (i = 0; i < m; i++)cin >> a[i].begin >> a[i].end >> a[i].value;
sort(a, a + m, cmp);
map<ll, ll>mm; mm.clear();
ll begin = a[0].begin, end = a[0].end, value = a[0].value;
for (i = 1; i < m; i++) {
//cout << a[i].begin << " " << a[i].end << " " << a[i].value << endl;
if (value != a[i].value) {
//cout << "1" << endl;
mm[begin]++;
mm[end+1]--;
begin = a[i].begin, end = a[i].end, value = a[i].value;
}
else if(a[i].begin-end>=2){
//cout << "2" << endl;
mm[begin]++;
mm[end + 1]--;
begin = a[i].begin;
end = a[i].end;
}
else {
//cout << "3" << endl;
begin = min(begin, a[i].begin);
end = max(end, a[i].end);
}
//cout << begin << " " << end << " " << value << endl << endl;
}
mm[begin]++;
mm[end + 1]--;
ll maxn = 0,flag=0;
for (i = 1; i <= n; i++) {
mm[i] += mm[i - 1];
//cout << i << " " << mm[i] << endl;
if (maxn < mm[i]) {
maxn = mm[i];
flag = i;
}
}
cout << flag << endl;
}
}