From f0ecc881cd3e8abfb2b7935071956acb0ba4c8bb Mon Sep 17 00:00:00 2001 From: Athul John Date: Thu, 15 Oct 2020 14:46:37 +0530 Subject: [PATCH] Added a few codeforce Problems --- codeforces/A/1420A.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++ codeforces/B/1420B.cpp | 79 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 codeforces/A/1420A.cpp create mode 100644 codeforces/B/1420B.cpp diff --git a/codeforces/A/1420A.cpp b/codeforces/A/1420A.cpp new file mode 100644 index 0000000..369e29a --- /dev/null +++ b/codeforces/A/1420A.cpp @@ -0,0 +1,85 @@ +/* +Codeforces Round #672 (Div. 2) +Problem: Wheatley decided to try to make a test chamber. He made a nice test chamber, +but there was only one detail absent — cubes. +For completing the chamber Wheatley needs n cubes. i-th cube has a volume ai. +Wheatley has to place cubes in such a way that they would be sorted in a non-decreasing order +by their volume. Formally, for each i>1, ai−1≤ai must hold. + +To achieve his goal, Wheatley can exchange two neighbouring cubes. It means that for any i>1 +you can exchange cubes on positions i−1 and i. +But there is a problem: Wheatley is very impatient. If Wheatley needs more than (n⋅(n−1))/2−1 +exchange operations, he won't do this boring work. +Wheatly wants to know: can cubes be sorted under this conditions? + +Input + +Each test contains multiple test cases. + +The first line contains one positive integer t +(1≤t≤1000), denoting the number of test cases. Description of the test cases follows. + +The first line of each test case contains one positive integer n +(2≤n≤5⋅104) — number of cubes. + +The second line contains n +positive integers ai (1≤ai≤109) — volumes of cubes. + +It is guaranteed that the sum of nover all test cases does not exceed 105. + +Output + +For each test case, print a word in a single line: "YES" (without quotation marks) if the cubes can +be sorted and "NO" (without quotation marks) otherwise. +*/ +/* +Solution Explanation: +We just need to find whether the array is strictly decreasing. If yes then to sort by swapping, (n*(n-1))/2 operations +will be needed, which is greater than (n*(n-1))/2 -1. +*/ + #include + #include + #include + #include + #include + + using namespace std; + + + void solve() + { + int n,flag=0; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + } + for(int j=n-1;j>0;j--) + { + if(arr[j]>=arr[j-1]) + { + flag=1; + break; + } + } + if(flag==1) + { + cout<<"YES\n"; + } + else + { + cout<<"NO\n"; + } + + } + + int main(){ + int t; + cin>>t; + while(t--) + { + solve(); + } + return 0; + } \ No newline at end of file diff --git a/codeforces/B/1420B.cpp b/codeforces/B/1420B.cpp new file mode 100644 index 0000000..c4b8ab6 --- /dev/null +++ b/codeforces/B/1420B.cpp @@ -0,0 +1,79 @@ +/* +Codeforces Round #672 (Div. 2) +Problem: Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask +Hermit Lizard for them. Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs +to solve the following task. +You are given a positive integer n, and an array a of positive integers. The task is to calculate +the number of such pairs (i,j) that i + #include + #include + #include + #include + + using namespace std; + + + void solve() + { + int n; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + } + sort(arr,arr+n); + long long itr=2; + long long ans=0; + long long tem=0; + for(int i=0;i=2) + { + ans+=(((tem-1)*tem)/2); + } + tem=0; + i--; + } + + } + if(tem>=2) + { + ans+=(((tem-1)*tem)/2); + + } + cout<>t; + while(t--) + { + solve(); + } + return 0; + } \ No newline at end of file