Fork me on GitHub

NYOJ题目1045看美女

--------------------------------------

开始的时候蠢蠢的使用直白的模拟:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         
 7         Scanner sc=new Scanner(System.in);
 8         
 9         int times=sc.nextInt();
10         while(times-->0){
11             
12             int n=sc.nextInt();
13             int a[]=new int[n];
14             for(int i=0;i<a.length;i++) a[i]=sc.nextInt();
15             
16             int ans=solve(a);
17             System.out.println(ans);
18         }
19     }
20     
21     public static int solve(int a[]){
22         int res=0;
23         for(int i=0;i<a.length;i++) if(canSee(a,i)) res++;
24         return res;
25     }
26     
27     public static boolean canSee(int a[],int i){
28         int t=i;
29         boolean ok=true;
30         while(--t>=0 && ok) if(a[t]>a[i]) ok=false;
31         if(ok) return true;
32         
33         ok=true; t=i;
34         while(++t<a.length) if(a[t]>a[i]) ok=false;
35         if(ok) return true;
36         
37         return false;
38     }
39     
40 }

 

然后很不幸,TLE

然后想着分别从两侧扫描只记录最大值然后跟当前比较做标记位就可以了,这样子的话最多扫描两遍数组就可以了,效率还是蛮高的。

 

AC代码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         
 7         Scanner sc=new Scanner(System.in);
 8         
 9         int times=sc.nextInt();
10         while(times-->0){
11             
12             int n=sc.nextInt();
13             int a[]=new int[n];
14             for(int i=0;i<a.length;i++) a[i]=sc.nextInt();
15             
16             int ans=solve(a);
17             System.out.println(ans);
18         }
19     }
20     
21     public static int solve(int a[]){
22         boolean book[]=new boolean[a.length];
23         
24         int max=a[0];
25         for(int i=0;i<a.length;i++){
26             if(!book[i] && a[i]>=max) book[i]=true;
27             max=Math.max(max,a[i]);
28         }
29         
30         max=a[a.length-1];
31         for(int i=a.length-1;i>=0;i--){
32             if(!book[i] && a[i]>=max) book[i]=true;
33             max=Math.max(max,a[i]);
34         }
35         
36         int res=0;
37         for(int i=0;i<book.length;i++) if(book[i]) res++;
38         return res;
39     }
40     
41 }

 

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=1045

posted @ 2016-09-14 03:42  CC11001100  阅读(207)  评论(0编辑  收藏  举报