D - Takahashi's Solitaire
https://atcoder.jp/contests/abc277/tasks/abc277_d
思路
先计算所有的输入的和 total,
将输入列表首先进行排列
找到所有连续段和中最大的值 maxsum, 此处连续满足条件 a[i] == a[i+1] 或者 a[i] + 1 == a[i+1]
最后 total - maxsum 即期望值。
Code
https://atcoder.jp/contests/abc277/submissions/36454757
long long n, m; vector<long long> a; int main() { cin >> n >> m; long long totalsum = 0; for(int i=0; i<n; i++){ int temp; cin >> temp; a.push_back(temp); totalsum += temp; } sort(a.begin(), a.end()); int continous = true; for(int i=0; i<n-1; i++){ if (a[i] == a[i+1] || a[i] + 1 == a[i+1]){ continue; } else { continous = false; break; } } if (continous == true){ cout << 0 << endl; return 0; } long long maxsum = -1; int size = a.size(); int last_index = size - 1; int first_index = 0; // treat first_index position long long zsum = 0; int zv = a[0]; zsum += zv; first_index++; int ii = first_index; while(a[ii] == a[ii - 1] || a[ii] == a[ii - 1] + 1){ zsum += a[ii]; ii++; first_index++; } if (zv == 0){ int lv = a[last_index]; if ((lv + 1) % m == 0){ zsum += lv; last_index--; int ii = last_index; while(a[ii] == a[ii+1] || a[ii] + 1 == a[ii+1]){ zsum += a[ii]; ii--; last_index--; } } } if (maxsum < zsum){ maxsum = zsum; } while(first_index <= last_index){ // search continium towards long long csum = 0; int cv = a[first_index]; csum += cv; first_index++; if (first_index > last_index){ if (maxsum < csum){ maxsum = csum; } break; } int ii = first_index; while(a[ii] == a[ii - 1] || a[ii] == a[ii - 1] + 1){ csum += a[ii]; first_index++; ii++; } if (maxsum < csum){ maxsum = csum; } } cout << totalsum - maxsum << endl; return 0; }
标签:ATCODER,index,--,long,ii,int,Solitaire,csum,maxsum From: https://www.cnblogs.com/lightsong/p/16887743.html