題目:
思路:
本題注意題目的條件即可,題意說一個摩天輪可以坐一個人或者兩個人,那么顯然我們就可以貪心一下
具體的,我們可以讓最小的去匹配最大的,如果此時大于 x,那么顯然我們根本無法使得 最大的那個存在兩個人共坐一個摩天輪的方案,因此最大的那個就肯定要單獨坐一輛,那么此時就看看次大的能不能和最小的匹配,以此類推,當能匹配時就直接跳出匹配過程即可,然后對次小值進行上訴操作即可
對于上訴過程,不難發現雙指針即可(代碼中的 solve 為另類寫法,忽視即可,主要在 solve2)
代碼:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());void solve2()
{int n,x;cin >> n >> x;vector<int> a(n);for (int i = 0; i < n; i++){cin >> a[i];}sort(a.begin(),a.end());int l = 0,r = n-1;int ans = 0;while (l <= r){while(r > l && a[l] + a[r] > x){r--;ans++;}l++,r--;ans++;}cout << ans << endl;
}void solve()
{int n, x, p;cin >> n >> x;multiset<int> mst;for (int i = 0; i < n; i++){cin >> p;mst.insert(p);}int ans = 0;while (!mst.empty()){if (*mst.begin() * 2 <= x){auto it = mst.upper_bound(x - *mst.begin());if (it != mst.begin()){it--;if (it != mst.begin()){mst.erase(it);}}}ans++;mst.erase(mst.begin());}cout << ans << endl;
}signed main()
{ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1;while (t--){solve2();}return 0;
}