寒假7-贪心

寒假7-贪心_第1张图片

#include
using namespace std;
#include
#include
struct bisai
{
	int s;
	int e;
};
bool Compare(bisai b1, bisai b2)
{
	return b1.e > b2.e;
}
int main()
{
	int n;
	cin >> n;
	vectorv;
	bisai b;
	for (int i = 1;i <= n;i++)
	{
		cin >> b.s >> b.e;
		v.push_back(b);
	}
	sort(v.begin(), v.end(), Compare);
	int ans;
	int tou;
	for (vector::iterator it = v.begin();it != v.end();it++)
	{
		if (it == v.begin())
		{
			ans = 1;
			tou = (*it).s;
		}
		else
		{
			if ((*it).e <= tou)
			{
				ans++;
				tou = (*it).s;
			}
			else if ((*it).s > tou)
			{
				tou = (*it).s;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

 寒假7-贪心_第2张图片

#include
using namespace std;
int arr[200010];
int main()
{
	int n, x;
	cin >> n >> x;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
	}
	long long ans = 0;
	for (int i = 1;i <= n;i++)
	{
		if (i == 1)
		{
			if (arr[i] > x)
			{
				ans += (arr[i] - x);
				arr[i] = x;
			}
		
		}
		else
		{
			if (arr[i] + arr[i - 1] > x)
			{
				ans += (arr[i] + arr[i - 1] - x);
				arr[i] -= (arr[i] + arr[i - 1] - x);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

 寒假7-贪心_第3张图片

#include
using namespace std;
int x[5010];
int y[5010];
int xy[5010][1010];
int main()
{
	int n, s;
	int a, b;
	cin >> n >> s >> a >> b;
	for (int i = 1;i <= n;i++)
	{
		cin >> x[i] >> y[i];
	}
	for (int i = 1;i <= n;i++)//xy[i][j]说明前i个apple时j力气的最优解
	{
		for (int j = 1;j <= s;j++)
		{
			xy[i][j] = xy[i - 1][j];
			if (x[i] <= a + b && y[i] <= j)
			{
				xy[i][j] = max(xy[i - 1][j], xy[i - 1][j - y[i]]+1);
			}
			//cout << xy[i][j] << " ";
		}
		//cout << endl;
	}
	cout << xy[n][s] << endl;
	return 0;
}

寒假7-贪心_第4张图片

#include
using namespace std;
#include
int a[30010];
int main()
{
	int w, n;
	cin >> w >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> a[i];
	}
	int e = n;
	int s = 1;
	int ans = 0;
	sort(a + 1, a + 1 + n);
	while (s <= e)
	{
		if (s == e)
		{
			ans++;
			break;
		}
		while (a[e] <= w)
		{
			if (a[e] + a[s] <= w)
			{
				a[e] += a[s];
				s++;
			}
			else if (a[e] + a[s] > w)
			{
				break;
			}
		}
		e--;
		ans++;

	}
	cout << ans << endl;
	return 0;
}

寒假7-贪心_第5张图片

#include
using namespace std;
#include
#include
struct cow
{
	int value;
	int output;
};
bool cow_compare(cow c1, cow c2)
{
	return c1.value < c2.value;
}
int main()
{
	int need, num;
	cin >> need >> num;
	cow c;
	vectorv;
	for (int i = 1;i <= num;i++)
	{
		cin >> c.value >> c.output;
		v.push_back(c);
	}
	sort(v.begin(), v.end(), cow_compare);
	long long ans = 0;
	for (vector::iterator it = v.begin();it != v.end();it++)
	{
		if (need >= (*it).output)
		{
			need -= (*it).output;
			ans += ((*it).value) * ((*it).output);
		}
		else if (need == 0)
		{
			break;
		}
		else if (need < (*it).output && need>0)
		{
			ans += need * (*it).value;
			need = 0;
		}
	}
	cout << ans << endl;
	return 0;
}

 寒假7-贪心_第6张图片

#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	int qian = 0;
	int ans = 0;
	int lu;
	for (int i = 1;i <= n;i++)
	{
		cin >> lu;
		if (lu > qian)
		{
			ans += (lu - qian);
			qian = lu;
		}
		else if (lu < qian)
		{
			qian = lu;
		}
	}
	cout << ans << endl;
	return 0;
}

 寒假7-贪心_第7张图片

#include
using namespace std;
int a[100100];
int s[100100];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> a[i];
	}
	long long ans = 0;
	int at, st;
	int ae, se;
	for (int i = 1;i < n;i++)
	{
		if (i == 1)
		{
			ans += a[1] + a[2];
			at = 3;
			ae = n;
			st = 1;
			se = 1;
			s[se] = a[1] + a[2];
		}
		else
		{
			if (ae > at && se > st)
			{
				if (s[st] + s[st + 1] <= a[at] + a[at + 1] && s[st] + s[st + 1] <= a[at] + s[st])
				{
					ans += s[st] + s[st + 1];
					st += 2;
					se++;
					s[se] = s[st] + s[st + 1];
				}
				else if (a[at] + a[at + 1] <= s[st] + s[st + 1] && a[at] + a[at + 1] <= a[at] + s[st])
				{
					ans += a[at] + a[at + 1];
					at += 2;
					se++;
					s[se] = a[at] + a[at + 1];
				}
				else if (a[at] + s[st] <= a[at] + a[at + 1] && a[at] + s[st] <= s[st] + s[st + 1])
				{
					ans += a[at] + s[st];
					st++;
					se++;
					at++;
					s[se] = a[at] + s[st];
				}
			}
			else if (ae < at)
			{
				ans += s[st] + s[st + 1];
				st += 2;
				se++;
				s[se] = s[st] + s[st + 1];
			}
			else if (at == ae && st == se)
			{
				ans += a[at] + s[st];
				st++;
				se++;
				at++;
				s[se] = a[at] + s[st];
			}
			else if (at == ae)
			{
				if (a[at]+s[st]<=s[st]+s[st+1])
				{
					ans += a[at] + s[st];
					st++;
					se++;
					at++;
					s[se] = a[at] + s[st];
				}
				else
				{
					ans += s[st] + s[st + 1];
					st += 2;
					se++;
					s[se] = s[st] + s[st + 1];
				}
			}
			else if (st == se)
			{
				if (a[at] + s[st] <= a[at] + a[at + 1])
				{
					ans += a[at] + s[st];
					st++;
					se++;
					at++;
					s[se] = a[at] + s[st];
				}
				else
				{
					ans += a[at] + a[at + 1];
					at += 2;
					se++;
					s[se] = a[at] + a[at + 1];
				}
			}
			
		}
	}
	cout << ans << endl;
	return 0;
}

你可能感兴趣的:(算法,数据结构,图论)