最后一题完全不会做,暴力都不会暴力,骗了30%。。。。。。。
A. 没啥难度,但是如果开vector[50000]这种C++会内存超限,非常恶心,所以直接用链表存了
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param m int整型 * @param a ListNode类 指向彩带的起点,val表示当前节点的val,next指向下一个节点 * @return ListNode类vector */ /*vector<int>vec[500005]; vector<ListNode*> solve(int m, ListNode* a) { // write code here while(a!=NULL){ vec[a->val%m].push_back(a->val); a = a->next; } vector<ListNode*>ans; for(int i=0;i<m;i++){ int size = vec[i].size(); if(size == 0) ans.push_back(NULL); else{ ListNode* head = new ListNode(-1); ListNode* tmp = head; for(int j=0;j<size;j++){ ListNode* p = new ListNode(vec[i][j]); tmp->next = p; tmp = p; } ans.push_back(head->next); } } return ans; }*/ vector<ListNode*>ans; map<int,ListNode*>t; vector<ListNode*> solve(int m, ListNode* a) { // write code here for(int i=0;i<m;i++){ ListNode* h = new ListNode(-1); ans.push_back(h); t[i] = h; } while(a!=NULL){ int dir = a->val%m; ListNode* n = new ListNode(a->val); ListNode* be = t[dir]; be->next = n; t[dir] = n; a = a->next; } for(int i=0;i<m;i++){ ListNode* be = ans[i]; ans[i] = be->next; } return ans; } };
B.N方过不了,标记一下到当前位置总共加了多少value即可,复杂度N
#include <bits/stdc++.h> using namespace std; #define ll long long int t; ll num[1005]; const ll mod = 1e9+7; int main() { scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&num[i]); sort(num+1,num+1+n); ll ans = 0; ll sum = 0; for(int i=n;i>=1;i--) { num[i] = (num[i] + sum)%mod; ans = (ans + num[i])%mod; sum = (sum + num[i])%mod; } printf("%lld\n",ans); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int t; int even[1005]; int odd[1005]; int main() { scanf("%d",&t); while(t--){ int n,m; scanf("%d%d",&n,&m); int oc = 0; int ec = 0; for(int i=1;i<=n;i++) { int tmp; scanf("%d",&tmp); if(tmp%2==1) odd[++oc] = tmp; else even[++ec] = tmp; } int ans = n; sort(even+1,even+1+ec); sort(odd+1,odd+1+oc); int l = 1,r=oc; while(l<r){ while(l<r){ if(odd[l]+odd[r]<=m) { ans--; r--; break; } r--; } l++; } l=1,r=ec; while(l<r){ while(l<r){ if(even[l]+even[r]<=m) { ans--; r--; break; } r--; } l++; } printf("%d\n",ans); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int n,k; int main() { scanf("%d%d",&n,&k); string str; cin>>str; string ans = ""; int len = str.length(); int now = 0; /*i 后面 还有 k-i 至少要留出k-i个字符 len - 1 len - k + i*/ for(int i=1;i<=k;i++){ char large = 'a' - 1; int dir; for(int j=now;j<len-k+i;j++){ if(large<str[j]){ large = str[j]; dir = j; } } ans.push_back(large); now = dir+1; } cout<<ans<<endl; return 0; }
全部评论
(9) 回帖