?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
28693823 |
Practice: rajat_27397 |
822C - 59 | GNU C++11 | Time limit exceeded on test 12 | 2000 ms | 15976 KB | 2017-07-19 13:47:32 | 2017-07-19 13:47:32 |
#include <bits/stdc++.h> #include <vector> #include <unordered_map> using namespace std; class node{ public: int start; int finish; int cost; //node* next; node(int x,int y,int z){ start=x; finish=y; cost=z; //next=NULL; } node(){ start=0; finish=0; cost=0; //next=NULL; } }; bool my(node i,node j){ return (i.start>j.start); } int main() { int n,m; cin >>n>>m; vector<node> vec(n); unordered_map<int,vector<node> > hash; int flag=0; for(int i=0;i<n;i++){ cin>>vec[i].start>>vec[i].finish>>vec[i].cost; } sort(vec.begin(),vec.end(),my); int ans=-1; for(int i=0;i<n;i++){ int tstart=vec[i].start; int tfinish = vec[i].finish; int tcost = vec[i].cost; if ( hash.find(tfinish-tstart+1) == hash.end() ){ vector<node> vn; vn.push_back( node(tstart,tfinish,tcost) ); hash[tfinish-tstart+1] = vn; } else{ vector<node> vn = hash[tfinish-tstart+1]; int j=0; vn.insert(vn.begin()+j,node(tstart,tfinish,tcost)); vn[0].cost = min(vn[0].cost,vn[1].cost); hash[tfinish-tstart+1] = vn; } } for(int i=0;i<n;i++){ int tstart=vec[i].start; int tfinish = vec[i].finish; int tcost = vec[i].cost; if ( hash.find(m-(tfinish-tstart+1)) != hash.end() ) { vector<node> vn = hash[m-(tfinish-tstart+1)]; int low =0; int high = vn.size()-1; int mid; while(low<=high){ mid = (low + high)/2; if(vn[mid].start>tfinish){ if(ans==-1){ ans=tcost+vn[mid].cost; } else{ ans=min(ans,tcost+vn[mid].cost); } high=mid-1; } else{ low=mid+1; } } } } cout<<ans<<endl; }
?
?
?
?