POJ 3298 Antimonotonicity
なんか縮めろといわれたので、縮めておいた。というのはうそで、講義中に縮めたんだけど。
普通の。
#include <iostream> #include <iterator> using namespace std; template<typename Iterator> static int solve(Iterator& in, int n) { int c = 1; int prev, x; prev = *in++; for (int i = 0; i < n - 1; i++) { x = *in++; if (c % 2 == 0 && x > prev || c % 2 == 1 && x < prev) { c++; } prev = x; } return c; } int main(int argc, char** argv) { int t, n; istream_iterator<int> it(cin); t = *it++; while (t--) { n = *it++; cout << solve(it, n) << endl; } return 0; }
なんとなく GNU Style & istream_iterator 使用。
短いの。
c; main(n,x,y) { for(gets(x);~scanf("%d%d",&n,&x);printf("%d\n",c)) for (c=1;--n&&scanf("%d",&y);x=y) c+=c&1^y>x; }
ループを圧縮してscanf をひとつにできそうな気もする。
ところで、mono-hateさんのは
c=c%2!=1^x>(y=s.nextInt())?c+1:c;
を
c+=c%2!=1^x>(y=s.nextInt())?1:0;
とするだけで 1 byte 縮まるような。でも、なんか for 文の終了条件がおかしい気が。