Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a few codeforces Problems #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions codeforces/A/1420A.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include<math.h>

using namespace std;


void solve()
{
int n,flag=0;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>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;
}
79 changes: 79 additions & 0 deletions codeforces/B/1420B.cpp
Original file line number Diff line number Diff line change
@@ -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<j and ai & aj≥ai⊕aj, where & denotes the bitwise AND operation,
and ⊕ denotes the bitwise XOR operation.
Danik has solved this task. But can you solve it?

Input
Each test contains multiple test cases.
The first line contains one positive integer t(1≤t≤10) denoting the number of test cases. Description
of the test cases follows.
The first line of each test case contains one positive integer n(1≤n≤105) — length of the array.
The second line contains n positive integers ai (1≤ai≤109) — elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For every test case print one non-negative integer — the answer to the problem.

*/
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include<math.h>

using namespace std;


void solve()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
long long itr=2;
long long ans=0;
long long tem=0;
for(int i=0;i<n;i++)
{
if(arr[i]<itr)
{
tem++;
}
else
{
itr*=2;
if(tem>=2)
{
ans+=(((tem-1)*tem)/2);
}
tem=0;
i--;
}

}
if(tem>=2)
{
ans+=(((tem-1)*tem)/2);

}
cout<<ans<<"\n";
}

int main(){
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}