貨物種類

貨物種類

鏈接: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;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章