9.3百度C++

tech2024-11-04  21

第一题暴力过。 第二题核心是二分法,我想直接调库所以存了很多个辅助数组再保存下标。整体逻辑是,要保证找到心仪角色人数最大,其实就是先给要求最低的人安排刚好不小于他要求的那个角色,再将这个角色erase掉。中间用了小技巧来记录下标。 第三题是三维dp,想转移方程想了半天,没写完,贴个java大佬的

有c艹版本的了。。

作者:牛客241045448号 链接:https://www.nowcoder.com/discuss/498038?type=post&order=time&pos=&page=2&channel=1009&source_id=search_post 来源:牛客网 ypedef long long ll; int main() { ll n, m; cin >> n >> m; vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(m + 1, vector<ll>(m + 1))); for (int i = 0; i <= min(n, m); ++i) { dp[i][i][0] = 1; } for (int hig = 0; hig <= n; ++hig) { int count = 0; for (int nxt = 0; nxt <= m; ++nxt) { for (int cur = 0; cur <= m; ++cur) { if (cur != nxt) { for (int pre = 0; pre <= m; ++pre) { if (pre != nxt && pre != cur && hig >= nxt + cur && hig - nxt >= cur + pre && dp[hig - nxt][cur][pre] != 0) { dp[hig][nxt][cur] = (dp[hig][nxt][cur] + dp[hig - nxt][cur][pre]) % 1000000007; count = (count + dp[hig - nxt][cur][pre]) % 1000000007; } } } } } if (hig == n) { if (n <= m) { count++; } cout << count; } } return 0; }

第二题:

#include<iostream> #include<vector> #include<queue> #include<unordered_map> #include<unordered_set> #include<algorithm> #include<string> #include<stack> #include<cmath> #include<set> #include<ctime> #include<map> #include<istream> #define LL long long #define L long using namespace std; int main() { int T; cin >> T; for (int t = 0; t<T; t++) { int n, m; cin >> n >> m; vector<pair<int, int>> goal(n, make_pair(0, 0)); vector<pair<int, int>> fact(m, make_pair(0, 0)); for (int i = 0; i<n; i++) { cin >> goal[i].first; goal[i].second = i; } for (int i = 0; i<m; i++) { cin >> fact[i].first; fact[i].second = i; } sort(goal.begin(), goal.end()); sort(fact.begin(), fact.end()); vector<int> ref(m, 0); int l = 0; for (auto itr = fact.begin(); itr != fact.end(); itr++,l++) { ref[l] = itr->first; } vector<int> res(n, -1); auto itref = goal.begin(); int bb = 0; for (int i = 0; i<n; i++) { auto itr = lower_bound(ref.begin(), ref.end(), goal[i].first); if (itr != ref.end()) { res[itref->second] = (fact.begin()+(itr-ref.begin())+bb)->second+1; itref++; ref.erase(itr); bb++; } } for (int k = 0; k<n; k++) { if (k == n - 1) { cout << res[k] << endl; break; } cout << res[k] << " "; } } return 0; }
最新回复(0)