pku 1944 Fiber Communications(枚举+贪心)
2009-12-04 22:52
232 查看
[code]如果去掉最后一个顶点可以和第一个顶点相连这个条件,那么该怎么解?
O(P)的贪心就可以了。
因为答案必定是无环的,那么枚举断点,然后采用贪心,就可以得到O(np)的解法。
O(P)的贪心就可以了。
因为答案必定是无环的,那么枚举断点,然后采用贪心,就可以得到O(np)的解法。
#include <string> #include <vector> #include <cmath> #include <queue> #include <map> #include <algorithm> #include <iostream> #define PI 3.14159265358979323846264338327950288 #define _clr(a,b) memset(a,b,sizeof(a)) template<class T> T _abs(T a) { if(a<0) return -a;return a;} template<class T> void get_min(T& a,T b) { if(a>b) a=b;} template<class T> void get_max(T& a,T b) { if(a<b) a=b;} using namespace std; struct node { int start,end; }nodes[30005]; bool operator<(node &a,node &b) { return a.start<b.start; } int N,P; int solve(int p,int q,int &nodeStart) { int right=-1,sum=0,pnode=nodeStart; while(nodes[pnode].start<p) pnode++; nodeStart=pnode; while(1) { if(nodes[pnode].start>=q||pnode>=3*P) break; if(nodes[pnode].end<q&&nodes[pnode].start>=p) { if(nodes[pnode].end>right) { if(nodes[pnode].start<=right) sum+=nodes[pnode].end-right; else sum+=nodes[pnode].end-nodes[pnode].start; right=nodes[pnode].end; } } pnode++; } return sum; } int main() { scanf("%d%d",&N,&P); int start,end,ans=INT_MAX,p=0; for(int i=0;i<P;i++) { scanf("%d%d",&start,&end); if(start>end) swap(start,end); nodes[i].start=start; nodes[i].end=end; nodes[i+P].start=end; nodes[i+P].end=start+N; nodes[i+2*P].start=start+N; nodes[i+2*P].end=end+N; } sort(nodes,nodes+3*P); for(int i=1;i<=N;i++) { ans=min(ans,solve(i,i+N,p)); } printf("%d/n",ans); return 0; }
相关文章推荐
- Bzoj 2241: [SDOI2011]打地鼠 暴力,枚举,贪心
- [PKU] 1012 Joseph [约瑟夫环,枚举]
- 简单的排序+贪心 pku 1089_Intervals
- codevs1001 舒适的路线 贪心枚举+并查集
- 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
- ZOJ 3715 Kindergarten Election(枚举、贪心)
- ZOJ 3715 Kindergarten Election(枚举+贪心)
- ZOJ 3715 Kindergarten Election(枚举+贪心)
- ZOJ-3715 Kindergarten Election(贪心+枚举)
- cf#22-B-Bargaining Table-枚举+贪心+dp(最大连续区间和)
- POJ 1042 钓鱼问题 贪心枚举及动态规划
- zoj1409 Communication System (贪心+枚举)
- Y2K Accounting Bug POJ - 2586 (贪心+枚举)
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
- 枚举合法字符使之按字典序输出——pku3049
- hdu5303(2015多校2)--Delicious Apples(贪心+枚举)
- 枚举+贪心 HDOJ 4932 Miaomiao's Geometry
- 选数字(贪心+枚举)
- POJ 1018 Communication System 贪心+枚举
- codeforces918C 917A The Monster 【贪心 + 思维枚举】