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.
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n,i;
- cin>>n;
- int a[n+9];
- for(i=0; i<n; i++)
- cin>>a[i];
- sort(a,a+n);
- if(a[n-1]>=a[n-2]+a[n-3])
- cout<<"NO"<<endl;
- else
- {
- cout<<"YES"<<endl;
- cout<<a[n-1]<<" "<<a[n-2]<<" ";
- for(i=n-4; i>=0; i--)
- cout<<a[i]<<" ";
- cout<<a[n-3]<<endl;
- }
- }
No comments