Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.MouseAdapter;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class ZigZagSequence {
- static int[] niza, dp;
- static int rec(int idx) {
- if(idx == niza.length - 1) {
- return 1;
- }
- if(dp[idx] != -1) {
- return dp[idx];
- }
- int result = 1;
- for(int i = idx + 1; i < niza.length; i++) {
- if(niza[idx] < 0) {
- if(niza[i] > 0) {
- result = Math.max(result, rec(i) + 1);
- }
- }
- else if(niza[idx] > 0) {
- if(niza[i] < 0) {
- result = Math.max(result, rec(i) + 1);
- }
- }
- }
- dp[idx] = result;
- return result;
- }
- static int najdiNajdolgaCikCak(int a[]) {
- niza = a;
- dp = new int[a.length];
- for(int i = 0; i < a.length; i++) {
- dp[i] = -1;
- }
- int result = 0;
- for(int i = 0; i < a.length; i++) {
- result = Math.max(result, rec(i));
- }
- return result;
- }
- public static void main(String[] args) throws Exception {
- int i,j,k;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- int a[] = new int[N];
- for (i=0;i<N;i++)
- a[i] = Integer.parseInt(br.readLine());
- int rez = najdiNajdolgaCikCak(a);
- System.out.println(rez);
- br.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement