羅区p1094のお土産パケット - 貪欲アルゴリズム

https://www.luogu.com.cn/problem/P1094
2人のお土産までのグループは、その後、一般的には、要件に準拠するために、小さなお土産を作ることができますが、素晴らしいお土産は唯一のグループとして単独ですることができ、これは間違いありませんセット数を増やし、非常に大きいのペアを構成するために、小さなお土産お土産を見つけようとします、これは最終的に最適解を取得します

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <iomanip>
using namespace std;
#define MAX 30005

int arr[MAX];
int t[MAX];



int main() {
#ifdef ONLINE_JUDGE
#else
	freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
	int n, w, num = 1, sum = 0;
	cin >> w >> n; 
	for (int i = 0; i < n; i++)cin >> arr[i];
	sort(arr, arr + n);
	int l = 0, r = n - 1;
	while (l <= r) {
		if (arr[l] + arr[r] <= w)
			l++, r--, sum++;
		else
			r--, sum++;
	}
	cout << sum << endl;
}
公開された52元の記事 ウォンの賞賛4 ビュー2203

おすすめ

転載: blog.csdn.net/qq_43941796/article/details/104292508