货物种类--------------------------------------差分+stl大法

在这里插入图片描述
在这里插入图片描述

解析:
差分+stl大法
用map存差分
对于每一个仓库 有<种类,个数> 所以用差分
然后枚举仓库,枚举仓库的种类

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10000;
map<int,int> v[N],now;
int n,m,l,r,d;
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d %d",&l,&r,&d);
		++v[l][d];
		--v[r+1][d];
	}
	
	int cnt=0,p;
	for(int i=1;i<=n;i++)//枚举每个货仓
	{
		for(auto it :v[i])
		{
			//it.first种类  it.second个数 
			now[it.first]+=it.second;//相当于差分的前缀和
			if(!now[it.first]) now.erase(it.first); //空的就删除
		}
		if(now.size()>cnt) //now.size()代表种类个数
		{
			cnt=now.size();
			p=i;
		}
	}
	cout<<p<<endl;
}
发布了491 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104756472