POJ-1230-Pass-Muraille
2013-09-16 19:04
399 查看
这个题属于贪心,就是说图中有一些墙,墙由坐标的起点和终点表示,然后要求求出至少要拆除多少墙,使得任意一列的墙不超过k个
思路:
贪心思路,每拆一个墙使得其在后面所剩下的列数长度最长,因为拆除最长的总可以使得后面减少墙数最多
代码:
思路:
贪心思路,每拆一个墙使得其在后面所剩下的列数长度最长,因为拆除最长的总可以使得后面减少墙数最多
代码:
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int maxn=104; int n,k,dp[maxn][maxn]; int main() { int T; scanf("%d",&T); while(T--) { memset(dp,0,sizeof(dp)); scanf("%d%d",&n,&k); int x=0,y=0; for(int i=1;i<=n;i++) { int x1,y1,x2,y2,len; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); x=max(x,max(x1,x2)); y=max(y,y1); len=max(x1,x2)-min(x1,x2)+1; for(int j=min(x1,x2);j<=max(x1,x2);j++) dp[y1][j]=max(dp[y1][j],len--); } int ans=0; for(int i=0;i<=x;i++) { int cnt=0; for(int j=0;j<=y;j++) if(dp[j][i]) cnt++; while(cnt>k) { int maxi=0,pos=0; for(int j=0;j<=y;j++) if(dp[j][i]>maxi) { maxi=dp[j][i]; pos=j; } int p=dp[pos][i]; for(int j=0;j<p;j++) dp[pos][i+j]=0; cnt--; ans++; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ 1230 Pass-Muraille (贪心)
- POJ-1230 Pass-Muraille
- poj 1230 Pass-Muraille( 贪心 )
- POJ 1230 Pass-Muraille 贪心
- POJ 1230 Pass-Muraille (贪心)
- poj 1230 Pass-Muraille 贪心
- poj 1230 Pass-Muraille
- POJ 1230 Pass-Muraille 贪心
- poj1230 Pass-Muraille (Greedy)
- POJ 1230 Pass-Muraille
- POJ_1230_Pass-Muraille
- (贪心5.1.1)POJ 1230 Pass-Muraille
- POJ 1230 Pass-Muraille 笔记
- poj 1230 Pass-Muraille
- (贪心5.1.1)POJ 1230 Pass-Muraille
- POJ 1230 Pass-Muraille
- POJ 1230 Pass-Muraille
- poj1230 Pass-Muraille 贪心算法,形成良好的代码风格很重要
- POJ 1230 Pass-Muraille#贪心+vector迭代器用法
- poj1230 贪心