• cf960F


    输入给出m条边,要求找到一条最长的路径满足边按照输入的顺序出现并且权值严格递增

    两种方法:第一种利用单调队列性质

    第二种利用数据结构优化

    #include<bits/stdc++.h>
    #define forn(i, n) for (int i = 0 ; i < int(n) ; i++)
    #define fore(i, s, t) for (int i = s ; i < (int)t ; i++)
    #define fi first
    #define se second
    #define all(x) x.begin(),x.end()
    #define pf2(x,y) printf("%d %d
    ",x,y)
    #define pf(x) printf("%d
    ",x)
    #define each(x) for(auto it:x)  cout<<it<<endl;
    #define pi pair<int,int>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+5;
    const int maxm=2e5+5;
    const int inf=1e9;
    vector<map<int,int>> s;
    int calc(int a,int w){
    	auto it=s[a].lower_bound(w);
    	if(it==s[a].begin()) return 1;
    	it--;
    	return (it->second)+1;
    }
    int main(){
    	int n,m;
    	cin>>n>>m;
    	s.resize(n+1);
    	int ans=0;
    	for(int i=0;i<m;i++){
    		int x,y,z;
    		cin>>x>>y>>z;
    		int val=calc(x,z);
    		if(calc(y,z+1)>val) continue;
    		s[y][z]=max(s[y][z],val);
    		auto it=s[y].upper_bound(z); 
    		while(!(it==s[y].end() || it->se>val))
    			it=s[y].erase(it);
    		ans=max(ans,val);
    	}	
    	cout<<ans<<endl;
    }
    

      

  • 相关阅读:
    css3记事
    ele
    vue记事1
    HBuilder
    继承与面向对象设计
    实现
    设计与声明
    资源管理
    构造/析构/赋值运算
    让自己习惯C++
  • 原文地址:https://www.cnblogs.com/033000-/p/12345491.html
Copyright © 2020-2023  润新知