ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P2255 [USACO14JAN]Recording the Moolympics S

2021-08-10 19:35:47  阅读:202  来源: 互联网

标签:en int 节目 P2255 Recording temp1 ans USACO14JAN dp


传送门

题目大意

FJ 要录制节目,每个节目具有一个指定的开始时间和结束时间。FJ具有两台摄像机,可以同时录制两个节目(FJ 一旦选择了某个节目来录制,就一定要录制完该节目)。请帮他确定录制的方案,使得 FJ 可以录制最多的节目,以提供给那些奶牛的粉丝们。

解题思路

贪心思路

贪心策略:

  • 按照结束时间排序。

  • 若节目可以放,则必定放。

  • 如果有一个节目两组都可以放,则放在结束时间长的那一组,因为可以使得另一组可能放得下时间更长的节目。

DP思路

首先我们将所有节目按照起始时间从小到大排序。这样是为了二分查找。

设 \(dp(i,j)\) 意为第一台摄像机正在考虑要不要拍摄第 \(i\) 个节目,第二台摄像机正在考虑要不要拍第 \(j\) 个节目。

每个 \(dp\) 都要考虑四种情况,分别是:拍 \(i\),不拍 \(i\),拍 \(j\),不拍 \(j\) 。

AC CODE

贪心

#include<bits/stdc++.h>
#define int long long
using namespace std;

long long n, ans;

struct abc
{
	long long st, en, id;
	bool operator < (const abc &tmp) const
	{
		if(en != tmp.en)
			return en < tmp.en;
		return st < tmp.st;
	}
}a[250];

signed main()
{
	scanf("%lld", &n);
	for(int i = 1; i <= n; ++i)
		scanf("%lld%lld", &a[i].st, &a[i].en);
	sort(a + 1, a + n + 1);
	int c1 = 0, c2 = 0;
	for(int i = 1; i <= n; ++i)
	{
		if(a[i].st >= c1)
		{
			c1 = a[i].en;
			ans++;
		}
		else if(a[i].st >= c2)
		{
			c2 = a[i].en;
			ans++;
		}
		if(c1 < c2) swap(c1, c2);
	}
	printf("%lld\n", ans);
	return 0;
}

DP

#include <bits/stdc++.h>
using namespace std;

struct Fastio
{
	Fastio operator>>(int &x)
	{
		x = 0;
		int f = 0;
		char c = getchar();
		while (c < '0' || c > '9')
			f |= c == '-', c = getchar();
		while (c >= '0' && c <= '9')
			x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
		x = f ? -x : x;
		return *this;
	}
	Fastio &operator<<(const char c)
	{
		putchar(c);
		return *this;
	}
	Fastio &operator<<(const char *str)
	{
		int cur = 0;
		while (str[cur])
			putchar(str[cur++]);
		return *this;
	}
	Fastio &operator<<(int x)
	{
		if (x == 0)
		{
			putchar('0');
			return *this;
		}
		if (x < 0) putchar('-'),x = -x;
		static int sta[45], top = 0;
		while (x)
			sta[++top] = x % 10, x /= 10;
		while (top)
			putchar(sta[top] + '0'), --top;
		return *this;
	}
}io;

int n;
int a[250];
int b[250];
int d[250][250];

struct Node
{
	int st, en;
} node[250];

bool cmp(Node a, Node b)
{
	return a.st < b.st;
}

int dp(int i, int j)
{
	if (i > j)
		swap(i, j);
	if (d[i][j])
		return d[i][j];

	int temp1, temp2, ans = 0;
	temp1 = lower_bound(&a[i] + 1, &a[n] + 1, b[i]) - a;
	temp1 = max(temp1, j + 1);
	temp2 = lower_bound(&a[j] + 1, &a[n] + 1, b[j]) - a;

	if (temp1 != n + 1)
	{
		ans = max(ans, dp(temp1, j) + 1);
	}

	if (temp2 != n + 1)
	{
		ans = max(ans, dp(i, temp2) + 1);
	}

	if (j != n)
	{
		ans = max(ans, dp(i, j + 1));
		ans = max(ans, dp(j + 1, j));
	}

	return d[i][j] = ans;
}

signed main()
{
	io >> n;
	for (int i = 1; i <= n; i++)
		io >> node[i].st >> node[i].en;
	sort(node + 1, node + n + 1, cmp);
	for (int i = 1; i <= n; i++)
	{
		a[i] = node[i].st;
		b[i] = node[i].en;
	}
	if (n == 1)
		io << 1 << "\n";
	else
		io << dp(1, 2) + 2 << "\n";
}

标签:en,int,节目,P2255,Recording,temp1,ans,USACO14JAN,dp
来源: https://www.cnblogs.com/orzz/p/15125466.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有