牛客小白月赛22 - 货物种类(差分)

 

链接: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≤n,m≤105,分别代表仓库的数目和进货的次数。
接下来 m 行,每行三个正整数l,r,d,1≤l,r≤n,1≤d≤109。编号在l和r之间的仓库收进编号为d的货物。

输出描述:

在一行中输出存放货物种类最多的仓库编号,若满足条件的仓库不止一个,则输出编号最小的那个。

输入

5 5
1 1 1
3 3 1
2 5 2
5 5 1
4 5 1

输出

3

 

区间种类和,比较明显的差分,线段树也能写

 

 1 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 const int INF=0x3f3f3f3f;
 4 const double eps =1e-8;
 5 const int mod=1e9+7;
 6 const int maxn=1e5+10;
 7 using namespace std;
 8 
 9 vector<int> st[maxn], ed[maxn];
10 map<int,int> mp;
11 
12 int main()
13 {
14     #ifdef DEBUG
15     freopen("sample.txt","r",stdin);
16     #endif
17     
18     int n,m;
19     scanf("%d %d",&n,&m);
20     for(int i=1;i<=m;i++)
21     {
22         int l,r,d;
23         scanf("%d %d %d",&l,&r,&d);
24         st[l].push_back(d);
25         ed[r + 1].push_back(d);
26     }
27     int id;
28     int MAX = 0;
29     int num = 0;
30     for(int i=1;i<=n;i++)
31     {
32         for(auto it : st[i])
33         {
34             if(mp[it]==0) num++;
35             mp[it]++;
36         }    
37         for(auto it : ed[i])
38         {
39             mp[it]--;
40             if(mp[it]==0) num--;
41         }
42         if(MAX < num)
43         {
44             MAX=num;
45             id=i;
46         }
47     }
48     printf("%d\n",id);
49     
50     return 0;
51 }

 

 

另一种写法:

 1 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 const int INF=0x3f3f3f3f;
 4 const double eps =1e-8;
 5 const int mod=1e9+7;
 6 const int maxn=1e5+10;
 7 using namespace std;
 8 
 9 vector<pair<int,int> > vt[maxn];
10 set<int> st;
11 multiset<int> sst;
12 
13 int main()
14 {
15     #ifdef DEBUG
16     freopen("sample.txt","r",stdin);
17     #endif
18     
19     int n,m;
20     scanf("%d %d",&n,&m);
21     for(int i=1;i<=m;i++)
22     {
23         int l,r,d;
24         scanf("%d %d %d",&l,&r,&d);
25         vt[l].push_back({1,d});
26         vt[r+1].push_back({-1,d});
27     }
28     int id;
29     int MAX = 0;
30     for(int i=1;i<=n;i++)
31     {
32         for(auto it : vt[i])
33         {
34             int op=it.first;
35             int d=it.second;
36             if(op==1)
37             {
38                 st.insert(d);
39                 sst.insert(d);
40             }
41             else if(op==-1)
42             {
43                 sst.erase(sst.find(d));
44                 if(sst.find(d)==sst.end()) st.erase(d);
45             }
46         }
47         if(MAX < st.size())
48         {
49             MAX=st.size();
50             id=i;
51         }
52     }
53     printf("%d\n",id);
54     
55     return 0;
56 }

 

 

 

 

 

-

posted @ 2020-03-24 03:27  jiamian22  阅读(390)  评论(0编辑  收藏  举报