第一题:链表颜色的
vector<ListNode*> solve(int m, ListNode* a) { unordered_map<int, pair<ListNode*, ListNode*>>mp; vector<ListNode*>val(m); while (a) { int index = (a->val) % m; if (mp.find(index) == mp.end()) { ListNode* node = new ListNode(a->val); mp[index] = pair<ListNode*, ListNode*>(node, node); } else { auto tail = mp[index].second; tail->next = new ListNode(a->val); mp[index].second = tail->next; } a = a->next; } for (int i = 0; i < m; i++) { if (mp.find(i) != mp.end()) val[i] = mp[i].first; } return val; }第二题:魔法球的,主要是大数问题
long long test(priority_queue<int>&data) { long long cur = 0; long long val = 0; while (!data.empty()) { long long top_num = data.top(); //cout << top_num << endl; val = (val + top_num + cur) % 1000000007; cur = (top_num + 2 * cur) % 1000000007; data.pop(); } return val % 1000000007; } int main() { int n, m; cin >> n; while (n--) { cin >> m; int tmp; priority_queue<int>data; for (int i = 0; i < m; i++) { cin >> tmp; data.push(tmp); } //cout<<data.size()<<endl; cout << test(data) << endl; } return 0; }第三题:刻舟求剑 贪心就完事了
int main() { int N; cin >> N; while (N--) { int n, w; cin >> n >> w; vector<int>data(n); vector<bool>index(n, false); for (int i = 0; i < n; i++)cin >> data[i]; sort(data.begin(), data.end()); int count = 0; for (int i = n - 1; i >= 0; i--) { if (data[i] == w) { count++; continue; } if (index[i])continue; bool flag = false; for (int j = i - 1; j >= 0; j--) { if ((!index[j]) && data[i] + data[j] <= w && ((data[i] + data[j]) % 2 == 0)) { count++; index[j] = true; flag = true; break; } } if (!flag)count++; } cout << count << endl; } return 0; }第四题:字典序的
int main() { vector<int>q(10000000); int N, k; cin >> N >> k; string s; cin >> s; int n = s.size(); int i = 0, head = 0, rear = -1; for (; i < n - k; i++) { while (head <= rear && s[q[rear]] < s[i])rear--; q[++rear] = i; } for (; i < n; i++) { while (head <= rear && s[q[rear]] < s[i])rear--; q[++rear] = i; cout << s[q[head++]]; } cout << endl; return 0; }第五题:请大佬指教,没啥思路,想到用暴力,但是还剩下10来分钟,感觉也来不及了
全部评论
(2) 回帖