首页 > 其他分享 >POJ - 3614 贪心

POJ - 3614 贪心

时间:2023-04-24 23:02:33浏览次数:52  
标签:3614 cow int long define POJ minf include 贪心

To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they’re at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn’t tan at all…

The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.

What is the maximum number of cows that can protect themselves while tanning given the available lotions?

Input

  • Line 1: Two space-separated integers: C and L
  • Lines 2…C+1: Line i describes cow i’s lotion requires with two integers: minSPFi and maxSPFi
  • Lines C+2…C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri

Output
A single line with an integer that is the maximum number of cows that can be protected while tanning

Sample Input
3 2
3 10
2 5
1 5
6 2
4 1
Sample Output
2

思路:将每个奶牛的minf按降序排序,然后在合法的情况下选择最大spf的防晒霜,
由贪心可知正确性显然

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 600005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-6
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
	ll ans = 1;
	a = a % mod;
	while (b > 0) {
		if (b % 2)ans = ans * a;
		b = b / 2;
		a = a * a;
	}
	return ans;
}

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a%b);
}

struct Cow {
	int minf, maxf;
}cow[maxn];

bool cmp(Cow a, Cow b) {
	return a.minf > b.minf;
}

int spf[maxn], cover[maxn];

int main()
{
	ios::sync_with_stdio(false);
	int c, l;
	cin >> c >> l;
	int i, j;
	for (i = 1; i <= c; i++) {
		cin >> cow[i].minf >> cow[i].maxf;
	}
	sort(cow + 1, cow + 1 + c, cmp);
	int tot = 0;
	for (i = 1; i <= l; i++) {
		cin >> spf[i] >> cover[i];
	}
	int pos;
	for (i = 1; i <= c; i++) {
		int maxx = -1;
		pos = -1;
		for (j = 1; j <= l; j++) {
			if (maxx < spf[j]) {
				if (spf[j] >= cow[i].minf&&spf[j] <= cow[i].maxf&&cover[j] >= 1) {
					maxx = spf[j];
					pos = j;
				}
			}
		}
		if (pos != -1) {
			cover[pos]--;
			tot++;
		}
	}
	cout << tot << endl;
}

标签:3614,cow,int,long,define,POJ,minf,include,贪心
From: https://blog.51cto.com/u_15657999/6222003

相关文章

  • 4.24 贪心法学习笔记
    多写题解多交流才能学好oi。在这里贴了代码,为了看上去完整一些。 大概是一些自己学习的记录罢。贪心不算客观意义上的算法,感觉还不算一种策略机制。我认为更像一种思路,其内涵就是择优,解题时就去想怎样才能更优。根据最优的思路能去做很多,如果说贪心是一个题的正解的话太抽......
  • codeforces 225B B. Well-known Numbers(数论+二分+贪心+构造)
    题目链接:codeforces225B题目大意:定义f(k,n)为类似菲波那契数推导,只不过变为前k项的和,然后给出一个数s,利用k-菲波那契数构造出一个不重复的集合的元素和为s,集合的规模大于1题目分析:首先因为菲波那契数的增长速度快的吓人,所以给的数据范围109很快就能达到,我们得到O(n)的构造出所有的......
  • UVA Children’s Game(贪心)
    Description4thIIUCInter-University ProgrammingContest,2005AChildren’sGameInput:standardinputOutput:standardoutputProblemsetter: Md. KamruzzamanTherearelotsofnumbergamesforchildren.Thesegamesareprettyeasytoplaybutnotsoeasyt......
  • POJ 1502 MPI Maelstrom(最短路)
    MPIMaelstromTimeLimit: 1000MS MemoryLimit: 10000KTotalSubmissions: 5476 Accepted: 3409DescriptionBIThasrecentlytakendeliveryoftheirnewsupercomputer,a32processorApolloOdysseydistributedsharedmemorymachinewithahierarchica......
  • POJ 2442 Sequence
    F- SequenceTimeLimit:6000MS     MemoryLimit:65536KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice POJ2442DescriptionGivenmsequences,eachcontainsnnon-negativeinteger.Nowwemayselectonenumber......
  • POJ3984 迷宫问题(BFS 记忆路径)
    迷宫问题TimeLimit:1000MS     MemoryLimit:65536KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice POJ3984SystemCrawler (2014-09-11)Description定义一个二维数组: intmaze[5][5]={0,1,0,0......
  • POJ 1789 Truck History
    TruckHistoryTimeLimit:2000MS     MemoryLimit:65536KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice POJ1789DescriptionAdvancedCargoMovement,Ltd.usestrucksofdifferenttypes.Sometrucksareused......
  • B. Tree Tag(贪心+树的最长直径)
    题目B.TreeTag题意思路因为这是一颗树,所以不管怎么追逐,我们都可以理解为在同一条路上追逐(去掉我们不走的路,就是一个线段)首先,如果da>db,显然能追上,进一步,da==db时,因为路径的长度是有限的,也显然可以追上因为树上任意两点的最短路径是固定的,所以a点可以一直朝着b追,而b是......
  • Permutation Restoration (贪心,排序处理) (范围左端点排序,然后取最小点放)
     思路:对于每一个bi都会有有一个范围,然后贪心的做,具体的先对这个范围按照左端点排序,然后贪心的去最小的值去放 ......
  • 贪心算法基础及leetcode例题
    理论本质:找到每个阶段的局部最优,然后去推导得到全局最优两个极端:常识&&很难:很多同学通过了贪心的题目,但都不知道自己用了贪心算法,因为贪心有时候就是常识性的推导,所以会认为本应该就这么做!套路:贪心没有套路,说白了就是常识性推导加上举反例做题的时候,只要想清楚局部最优......