poj 1230 Pass-Muraille 贪心

题意:

给一些平行于x轴的墙,求最少去掉多少墙使得x轴上每点穿过不超过k面墙。

分析:

一开始还以为是重复覆盖问题(取最少的行使每列至少一个1),其实这题是可以用贪心解的(类似取最多的行使每列至多k个1)。

代码:

//poj 1230
//sep9
#include <iostream>
using namespace std;
const int maxN=128;
int n,max_k,maxx,maxy;
int g[maxN][maxN];

int work()
{
	int ans=0;
	for(int j=0;j<=maxx;++j){
		int sum=0;
		for(int i=0;i<=maxy;++i)
			sum+=(g[i][j]>0?1:0);
		while(sum>max_k){
			int index,t_sum=-1;
			for(int k=0;k<=maxy;++k){
				int r_sum=0;
				if(g[k][j]==0) continue;
				for(int x=j;x<=maxx&&g[k][x]==g[k][j];++x)
					++r_sum;
				if(r_sum>t_sum){
					t_sum=r_sum;
					index=k;
				}
			}
			++ans;
			for(int x=j,tmp=g[index][j];x<=maxx&&g[index][x]==tmp;++x)
				g[index][x]=0;
			--sum;
		}
	}
	return ans;
}

int main()
{
	int cases;
	scanf("%d",&cases);
	while(cases--){
		scanf("%d%d",&n,&max_k);
		memset(g,0,sizeof(g));
		maxx=-1,maxy=-1;
		for(int t=1;t<=n;++t){
			int x1,x2,y;
			scanf("%d%d%d%d",&x1,&y,&x2,&y);
			if(x2<x1) swap(x1,x2);
			maxx=max(maxx,x2);
			maxy=max(maxy,y);
			for(int i=x1;i<=x2;++i)
				g[y][i]=t;
		}
		printf("%d\n",work());
	}
	return 0;
}
时间: 2024-10-13 14:52:46

poj 1230 Pass-Muraille 贪心的相关文章

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

POJ 3085 Quick Change (贪心)

Quick Change Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5801   Accepted: 4175 Description J.P. Flathead's Grocery Store hires cheap labor to man the checkout stations. The people he hires (usually high school kids) often make mistak

POJ 1659 Frogs&#39; Neighborhood (贪心)

题意:中文题. 析:贪心策略,先让邻居多的选,选的时候也尽量选邻居多的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring>

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆. 看代码吧,应该很清晰直观的了. 效率是O(n),虽然有嵌套循环,但是下标没有重复,一遍循环就可以了,故此是O(n). #include <stdio.h> #include <cmath> #incl

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

Poj 1328 Radar Installation 贪心

题目链接:http://poj.org/problem?id=1328 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52768   Accepted: 11867 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the othe

POJ 3190 Stall Reservations贪心

POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obvi

[ACM] POJ 1328 Radar Installation (贪心,区间选点问题)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51131   Accepted: 11481 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ 2392 Space Elevator(贪心+多重背包)

POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木,每种积木都有一个高度h[i],一个数量num[i],还有一个限制条件,这个积木所在的位置不能高于limit[i],问能叠起的最大高度? 分析: 本题是一道多重背包问题, 不过每个物品的选择不仅仅要受该种物品的数量num[i]限制, 且该物品还受到limit[i]的限制. 这里有一个贪心的结论: 我们每次背包选取物品时都应该优先放置当前limi