Codeforces 1189B. Number Circle Solution

 Explain:This question mainly relies on ideas, it is easy to think of the correct method, and the amount of code is not much.

For this group of numbers, we can sort them in ascending order first, and then compare the last three digits~~ (why, look down)~~, for example, the last three digits are a[n-2],a[n-1],a[n ], then compare whether a[n]>a[n-1]+a[n-2] is established, if not, it means that it must not form a ring, and the backhand is to output NO; if it is established, first combine a[n] and a [n-1] output, and then output the other numbers except a[n-2] directly, and output a[n-2] finally;
namely a[n] a[n-1] a[1] a[2 ] …A[n-3]
Because we have sorted, so a[1] to a[n-3] are all in ascending order, and the sum of the numbers on both sides is absolutely greater than itself (the number on the right is greater than), and because We have judged that so a[n]<a[n-1]+a[n-2], and a[n] is the largest number, a[n-1], a[n-2] are both less than it, So it is also satisfied, so that all numbers are satisfied.

Solution in C++:

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh  

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,i;
  6. cin>>n;
  7. int a[n+9];
  8. for(i=0; i<n; i++)
  9. cin>>a[i];
  10. sort(a,a+n);
  11. if(a[n-1]>=a[n-2]+a[n-3])
  12. cout<<"NO"<<endl;
  13. else
  14. {
  15. cout<<"YES"<<endl;
  16. cout<<a[n-1]<<" "<<a[n-2]<<" ";
  17. for(i=n-4; i>=0; i--)
  18. cout<<a[i]<<" ";
  19.  
  20. cout<<a[n-3]<<endl;
  21.  
  22. }
  23.  
  24. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.