牛客 - 货物种类(差分)
生活随笔
收集整理的這篇文章主要介紹了
牛客 - 货物种类(差分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:有 n 個倉庫,接下來有 m 次進貨,每次進貨會向[ l , r ]區間內的倉庫進入種類為 w 的貨物,最后問哪個倉庫的貨物種類最多
題目分析:看到是區間問題,下意識想到了線段樹,但實際上線段樹處理這種不同顏色的問題一般都是狀態壓縮,但顯然這個題目貨物的種類范圍到達了1e9,所以肯定不是顏色統計的模板題了,比賽的時候差分的想法在腦中浮現過一次,但很快就被自己否決了,原因還是因為自己不會呀,賽后補提的時候看了一下大佬們的代碼,恍然大悟,差分+一個map維護一下區間內的出現次數就好了,非常簡單的一道思維題
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;vector<int>add[N],del[N];map<int,int>vis;int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n,m;scanf("%d%d",&n,&m);while(m--){int l,r,w;scanf("%d%d%d",&l,&r,&w);add[l].push_back(w);del[r+1].push_back(w);}int mmax=-inf,mark,cnt=0;for(int i=1;i<=n;i++){for(auto j:add[i]){if(vis[j]==0)cnt++;vis[j]++;}for(auto j:del[i]){vis[j]--;if(vis[j]==0)cnt--;}if(cnt>mmax){mmax=cnt;mark=i;}}printf("%d\n",mark);return 0; }?
總結
以上是生活随笔為你收集整理的牛客 - 货物种类(差分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客 - 收集纸片(最短哈密顿路径-状压
- 下一篇: 牛客 - 仓库选址(中位数+思维)