标签:
题目链接:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24379 | Accepted: 9274 |
Description
Input
Output
Sample Input
5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
Sample Output
6
题意:
给一个区间[ai,bi],这个区间里有ci个相同的元素,问这个集合最少包含多少元素;
思路:
可以得到这样的几个不等式;
c[i]<=dis[b+1]-dis[a];
0<=dis[i+1]-dis[i];
-1<=dis[i]-dis[i+1];
题意是一定要满足这些条件,那么就是找最长路;当然也可以变成>=来建图,求最短路;
AC代码:
//最长路径的代码
//#include <bits/stdc++.h> #include <iostream> #include <queue> #include <cmath> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=5e4+5; int n,head[3*N],vis[N],ans[N],cnt,mmin; struct Edge { int to,next,dis; }edge[3*N]; queue<int>qu; void spfa() { mst(vis,0); mst(ans,-inf); ans[mmin]=0; qu.push(mmin); vis[mmin]=1; while(!qu.empty()) { int fr=qu.front(); qu.pop(); for(int i=head[fr];i!=-1;i=edge[i].next) { int x=edge[i].to; if(ans[x]<ans[fr]+edge[i].dis) { ans[x]=ans[fr]+edge[i].dis; if(!vis[x]) { qu.push(x); vis[x]=1; } } } vis[fr]=0; } } void add_edge(int s,int e,int val) { edge[cnt].to=e; edge[cnt].next=head[s]; edge[cnt].dis=val; head[s]=cnt++; } int main() { while(scanf("%d",&n)!=EOF) { int a,b,c,mmax=0; mmin=55000; cnt=0; mst(head,-1); Riep(n) { scanf("%d%d%d",&a,&b,&c); mmax=max(mmax,b); mmin=min(mmin,a); add_edge(a,b+1,c); } Riep(mmax)add_edge(i,i+1,0),add_edge(i+1,i,-1); spfa(); printf("%d\n",ans[mmax+1]); } return 0; }
//最短路径的代码 //#include <bits/stdc++.h> #include <iostream> #include <queue> #include <cmath> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=5e4+5; int n,head[3*N],vis[N],ans[N],cnt,mmin,mmax; struct Edge { int to,next,dis; }edge[3*N]; queue<int>qu; void spfa() { mst(vis,0); mst(ans,inf); ans[mmax]=0; qu.push(mmax); vis[mmax]=1; while(!qu.empty()) { int fr=qu.front(); qu.pop(); for(int i=head[fr];i!=-1;i=edge[i].next) { int x=edge[i].to; if(ans[x]>ans[fr]+edge[i].dis) { ans[x]=ans[fr]+edge[i].dis; if(!vis[x]) { qu.push(x); vis[x]=1; } } } vis[fr]=0; } } void add_edge(int s,int e,int val) { edge[cnt].to=e; edge[cnt].next=head[s]; edge[cnt].dis=val; head[s]=cnt++; } int main() { while(scanf("%d",&n)!=EOF) { int a,b,c; mmax=0; mmin=55000; cnt=0; mst(head,-1); Riep(n) { scanf("%d%d%d",&a,&b,&c); mmax=max(mmax,b+1); mmin=min(mmin,a); add_edge(b+1,a,-c); } Riep(mmax)add_edge(i,i+1,1),add_edge(i+1,i,0); spfa(); printf("%d\n",-ans[mmin]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5475999.html