日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Cheese 模拟,贪心(300)

發布時間:2025/3/19 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Cheese 模拟,贪心(300) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


題意 :

  • 有n種芝士,第i種芝士每克有aia_iai?的價值,最多只能拿bib_ibi?克,問在最多拿w克的情況下,總的價值和最大是多少

思路 :

  • 貪心,從單位價值最高的拿起
  • 對于每種芝士的數量限制,我們可以每次取min(w,b_i),其中w是剩余可以用的克數
  • 用vector而不用數組,減少空間
  • 對vector進行輸入時,用&,就可以了
  • 對pair<int, int>進行排序,當需要從大到小,可以reverse
#include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <unordered_set> #include <math.h> #define endl '\n' #define fi first #define se second #define pb push_backusing namespace std; using ll = long long;typedef pair<int, int> PII;int main() {cin.tie(nullptr) -> sync_with_stdio(false);ll n, w; cin >> n >> w;vector<pair<long long, long long>> v(n);for (auto &nx : v)cin >> nx.fi >> nx.se;sort(v.begin(), v.end());reverse(v.begin(), v.end());ll ans = 0;for (auto &nx : v){ans += nx.fi * min(w, nx.se);w -= min(w, nx.se);}cout << ans << endl;return 0; }

總結

以上是生活随笔為你收集整理的Cheese 模拟,贪心(300)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。