货物种类

链接:https://ac.nowcoder.com/acm/contest/4462/H
来源:牛客网

某电商平台有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 <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef pair<int, int> PII;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1e5 + 10;
const int M = 35;
const int mod = 1e9 + 7;
const int PP = 131;
const double eps = 1e-10;

struct Node{
	int l, r, d;
	bool operator<(const Node &W)const{
		if (d == W.d)   return l < W.l;
		else  return d < W.d;
   	}
}node[N];

set<PII> ss;
int cnt[N];
bool st[N];
signed main(){
	int n, m;
	gt(n), gt(m);
	
	for (int i = 1; i <= m; i ++){
		scanf("%lld%lld%lld", &node[i].l, &node[i].r, &node[i].d);
	}
	
	sort(node + 1, node + 1 + m);
	
	int st = node[1].l, ed = node[1].r, last = node[1].d;
	for (int i = 2; i <= m; i ++){
		if (node[i].d == last && node[i].l <= ed){
			ed = max(ed, node[i].r);
		}
		else{
			cnt[st] += 1;
			cnt[ed + 1] -= 1;
			st = node[i].l;
			ed = node[i].r;
			last = node[i].d;
		}
	}
	
	cnt[st] += 1;
	cnt[ed + 1] -= 1;
	
	int ans = 0;
	
	for (int i = 1; i <= n; i ++){
		cnt[i] += cnt[i - 1];
	//	cout << i << "----------" << cnt[i] << endl;
		ans = max(ans, cnt[i]);
	}
	
//	cout << ans << endl;
	for (int i = 1; i <= m; i ++){
		if (cnt[i] == ans){
			cout << i << endl;
			break;
		}
	}
	
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值