货物种类
题目描述
某电商平台有n个仓库,编号从1到n。
当购进某种货物的时候,商家会把货物分散的放在编号相邻的几个仓库中。
我们暂时不考虑售出,你是否能知道,当所有货物购买完毕,存放货物种类最多的仓库编号为多少?
思路:
对于每一个仓库,都有<货物种类,对应数量>,对于这个对应数量我们采用差分!
于是我们就可以枚举仓库,枚举每个仓库的货物种类。
(不知道为什么理解也理解了很久,写又写不出来为什么,因为明白过后感觉就是那个样子qaq,太难了)
AC CODE
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int read()
{
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); }
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int maxN = 100005;
int n, m;
map<int, int>mp[maxN], now;
int main()
{
n = read(); m = read();
for(int i = 1; i <= m; ++ i )
{
int l, r, d;
l = read(); r = read(); d = read();
++ mp[l][d];
-- mp[r + 1][d];
}
int cnt = 0, id = 0;
for(int i = 1; i <= n; ++ i ) //枚举每个位置
{
for(auto it : mp[i]) //每个位置有一个数组d[](哪种货物)num(货物的数量)
{
now[it.first] += it.second; // 货物it.first有it.second种
if(!now[it.first]) //该货物没了,踢出去
now.erase(it.first);
}
if(now.size() > cnt)
{
cnt = now.size();
id = i;
}
}
printf("%d\n", id);
return 0;
}