这场比赛还是挺好的,C题没被hack但是最后system test的时候fail了,,另外E是个线段树,这个我得去先做一些线段树专题了回来再补
首先是A题(DZY Loves Hash) ,是个明显的水题,对于输入的n个x如果 x % p 对应的值已经出现过,就输出 第一次出现冲突的x的编号, 若没有冲突则输出 -1;
用一个大小为p的数组来记录对应的模值是否出现过即可
代码如下:
Result : Accepted Memory : 4 KB Time : 31 ms
/* * Author: Gatevin * Created Time: 2014/7/13 21:01:47 * File Name: A.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; int p,n; bool se[330]; int main() { cin>>p>>n; memset(se, 0 , sizeof(se)); int tmp; for(int i = 1; i <= n; i++) { cin>>tmp; tmp %= p; if(!se[tmp]) { se[tmp] = true; } else { cout<<i<<endl; return 0; } } cout<<"-1"<<endl; return 0; }
根据贪心的思想,将26个字母当中值最大的字母放在整个字符的末尾,连续放k个会使得字符串的和最大
代码如下:
Result : Accepted Memory : 8 KB Time : 31 ms
/* * Author: Gatevin * Created Time: 2014/7/13 21:07:55 * File Name: B.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; int ma[30]; string s; int k; int main() { cin>>s; cin>>k; int ans = 0; int tmp = 0; for(int i = 1; i <= 26; i++) { cin>>ma[i]; tmp = max(tmp,ma[i]); } for(int i = 0; i <= s.length() - 1; i++) { ans += ma[s[i] - 'a' + 1]*(i + 1); } for(int i = 1; i <= k; i++) { ans += (i + s.length()) * tmp; } cout<<ans<<endl; return 0; }
此题就是细节蛮多的吧,容易出错
代码如下:
Result : Accepted Memory : 400 KB Time : 93 ms
/* * Author: Gatevin * Created Time: 2014/7/15 12:44:16 * File Name: test.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; int n; int a[100100]; int main() { cin>>n; for(int i = 1; i <= n; i++) { cin>>a[i]; } int last1,last2; int cnt1 = 0, cnt2 = 0; int now = 0; int answer = 0; a[0] = 0; while(now < n) { if(a[now] < a[now + 1]) { cnt1++; last1 = now + 1; } else { answer = max(answer, cnt1 + 1); cnt2 = 1; last2 = now + 1; for(int i = now + 1; i < n; i++) { if(a[i] < a[i + 1]) { cnt2++; last2 = i + 1; } else { break; } } if(now >= 2 && a[now + 1] - a[now - 1] >= 2) { answer = max(answer, cnt1 + cnt2); } else { answer = max(answer, cnt1 + 1); answer = max(answer, cnt2 + 1); } if(now <= n - 2 && a[now + 2] - a[now] >= 2) { answer = max(cnt1 + cnt2, answer); } else { answer = max(answer, cnt2 + 1); answer = max(answer, cnt1 + 1); } cnt1 = cnt2; last1 = last2; now = last2; continue; } now++; } if(cnt1 == n) answer = n; cout<<answer<<endl; return 0; }
然后将这个提出来之后,就只需要用优先队列找出去 i 次行得到的最大值和 取 i 次列得到的最大值即可;这里需要预处理,不然在枚举 k 时将会超时
代码如下:
Result : Accepted Memory : 15696 KB Time : 327 ms
/* * Author: Gatevin * Created Time: 2014/7/17 14:20:01 * File Name: test.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; const lint inf = -99999999999999999; lint row,col,k,p; lint sumr[1001]; lint sumc[1001]; lint answerR[1000001]; lint answerC[1000001]; priority_queue <lint> Rtmp,Ctmp; int main() { scanf("%I64d %I64d %I64d %I64d",&row, &col, &k, &p); memset(sumr, 0, sizeof(sumr)); memset(sumc, 0, sizeof(sumc)); lint tmp; for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) { scanf("%I64d", &tmp); sumr[i] += tmp; sumc[j] += tmp; } } for(int i = 1; i <= row; i++) { Rtmp.push(sumr[i]); } for(int i = 1; i <= col; i++) { Ctmp.push(sumc[i]); } memset(answerR, 0, sizeof(answerR)); memset(answerC, 0, sizeof(answerC)); lint ans = 0; for(int j = 1; j <= k; j++) { ans += Rtmp.top(); tmp = Rtmp.top(); tmp -= col*p; Rtmp.pop(); Rtmp.push(tmp); answerR[j] = ans; } ans = 0; for(int j = 1; j <= k; j++) { ans += Ctmp.top(); tmp = Ctmp.top(); tmp -= row*p; Ctmp.pop(); Ctmp.push(tmp); answerC[j] = ans; } lint answer = inf; for(int i = 0; i <= k; i++) { answer = max(answer, answerR[i] + answerC[k - i] - i*(k - i)*p); } printf("%I64d\n",answer); return 0; }