UVa413 - Up and Down Sequences

#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

void print(int a)
{
    cout << a <<  " ";
}

int main()
{
    string s;
    int num;
    int n;
    int cur;
    int cnt;
    int equal;
    int up, down;

#ifndef ONLINE_JUDGE
    //freopen("uva_in.txt", "r", stdin);
    ifstream cin("uva_in.txt");
#endif

    while (getline(cin, s)) {
        if (s == "0") break;

        istringstream line(s);
        vector<int> v;
        vector<int> ans[2];

        n = 0;
        while (line >> num, num) {
            v.push_back(num);
            n++;
        }

        cur = -1;    
        cnt = 0;
        equal = 0;
        up = down = 0;
        //for_each(v.begin(), v.end(), print);
        for (size_t i = 1; i < v.size(); i++) {
            if (cur == -1) {
                if (v[i] > v[i - 1]) {
                    cur = 0;
                    up++;
                } else if (v[i] == v[i - 1]) {
                    equal++;
                } else if (v[i] < v[i - 1]) {
                    cur = 1;
                    down++;
                }
            } else {
                if (v[i] == v[i - 1]) equal++;
                else {
                    if (!cur && v[i] < v[i - 1]) {
                        ans[cur].push_back(up + equal);
                        cur = 1 - cur;
                        equal = 0;
                        up = 0;
                        down = 1;
                    } else if (!cur && v[i] > v[i - 1]) {
                        up++;
                    } else if (cur && v[i] > v[i - 1]) {
                        ans[cur].push_back(down + equal);
                        cur = 1 - cur;
                        equal = 0;
                        down = 0;
                        up = 1;
                    } else {
                        down++;
                    }
                }
            }
        }
        
        if (cur == -1) {
            ans[0].push_back(0);
            ans[1].push_back(0);
        } else if (cur == 0) {
            ans[0].push_back(equal + up);
            if (ans[1].size() == 0) ans[1].push_back(down);
        } else {
            if (ans[0].size() == 0) ans[0].push_back(up);
            ans[1].push_back(equal + down);
        }
        cout.precision(6);
        //cout << "ans[0].size():" << ans[0].size() << endl;
        //cout << "ans[1].size():" << ans[1].size() << endl;
        cout << "Nr values = " << n << ":  ";
        if (cur == -1) {
            cout << fixed << 0.0 << " " <<  fixed << 0.0 << endl;
        } else {
            double ans1 = 0, ans2 = 0;
            //for_each(ans[0].begin(), ans[0].end(), print);
            //cout << endl;
            //for_each(ans[1].begin(), ans[1].end(), print);
            //cout << endl;
            for (size_t i = 0; i < ans[0].size(); i++) {
                ans1 += ans[0][i];
            }
            for (size_t i = 0; i < ans[1].size(); i++) {
                ans2 += ans[1][i];
            }

            ans1  /= ans[0].size();
            ans2 /= ans[1].size();
            cout << fixed << ans1 << " " << fixed << ans2 << endl;
        }
    }
    return 0;
}


你可能感兴趣的:(UVa413 - Up and Down Sequences)