C - Merge the balls
C - Merge the balls
https://atcoder.jp/contests/abc351/tasks/abc351_c
思路
使用stack 记录 序列路径
对栈顶两个元素 尝试做缩减处理。
Code
https://atcoder.jp/contests/abc351/submissions/52873456
int n; stack<long long> sq; int main() { cin >> n; for(int i=0; i<n; i++){ long long temp; cin >> temp; if (sq.size() == 0){ sq.push(temp); } else { sq.push(temp); while(sq.size() >= 2){ int topfirst = sq.top(); sq.pop(); int topsecond = sq.top(); sq.pop(); if (topfirst == topsecond){ sq.push(topfirst+1); } else { sq.push(topsecond); sq.push(topfirst); break; } } } } cout << sq.size() << endl; return 0; }
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。