Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

java - What is the difference between in these two implementation (Codeforces 1472D)?

While doing the coding problem "https://codeforces.com/problemset/problem/1472/D" as i was getting the error during evaluation

       "Time limit exceeded on test 10"

for the code

static void solve(long a[]){
        Arrays.sort(a);
        long al=0,bo=0;
        int t=0;
        for(int i=a.length-1;i>=0;i--){
            if(t==0 && a[i]%2==0)   al+=a[i];
            else if(t==1 && a[i]%2!=0)  bo+=a[i];

            t=(t+1)%2;
        }

        if(al>bo)   System.out.println("Alice");
        else if(al<bo)  System.out.println("Bob");
        else    System.out.println("Tie");
    }

I went to the leaderboards to check out how others had answered the question in my programming language (java) and i stumbled upon the solution "https://codeforces.com/problemset/submission/1472/103564414"

Taking out the relevant part in this code :

            Arrays.sort(arr);
            Collections.reverse(Arrays.asList(arr));
            long ans = 0;
            for (int i = 0; i < n; i++) {
                if (i % 2 == 0 && arr[i] % 2 == 0) {
                    ans += arr[i];
                } else if (i % 2 == 1 && arr[i] % 2 == 1) {
                    ans -= arr[i];
                }
            }
            if (ans > 0) {
                System.out.println("Alice");
            } else if (ans == 0) {
                System.out.println("Tie");
            } else {
                System.out.println("Bob");
            }

I am a bit confused as to how the other person person's code is passing the tests even though even my approach is almost identical(as per my knowledge).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...