Advertisement
Guest User

zigzag

a guest
Feb 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("zigzag.in");
  9. ofstream fout("zigzag.out");
  10.  
  11. int nr[100],sgn[100],lst[100];
  12. vector<int>inp;
  13.  
  14. int getsgn(int x){
  15.     if(x<0)return -1;
  16.     else return 1;
  17. }
  18.  
  19. int main()
  20. {
  21.     int n,x;
  22.     fin>>n;
  23.     for(int i=0;i<n;i++){
  24.         fin>>x;
  25.         inp.push_back(x);
  26.     }
  27.     lst[0] = inp[0];
  28.     for(int i=1;i<inp.size();i++){
  29.         for(int j=0;j<i;j++){
  30.             if(sgn[j]==0){
  31.                 if(nr[j]+1>nr[i]){
  32.                     nr[i] = nr[j]+1;
  33.                     sgn[i] = getsgn(inp[i]-lst[j]);
  34.                     lst = inp[i];
  35.                 }
  36.             }
  37.             else
  38.             if(sgn[j] == -getsgn(inp[i]-lst[j]) && nr[j]+1>nr[i]){
  39.                 nr[i] = nr[j]+1;
  40.                 sgn[i] = -sgn[j];
  41.                 lst = inp[i];
  42.             }
  43.  
  44.         }
  45.     }
  46.  
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement