We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: C0de_Beast
Given $$$n$$$ chocolates and $$$m$$$ students:
- If $$$n$$$ is less than $$$m$$$, then there aren't enough chocolates for every student, so we will print $$$-1$$$.
- If $$$n$$$ is equal to or greater than $$$m$$$, then every student will receive one chocolate each and the remainder will be returned to the school. The number of chocolates to be returned will be $$$n - m$$$.
#include<stdio.h>
using namespace std;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
if(n<m) {
printf("-1\n");
}else {
printf("%d\n",n-m);
}
return 0;
}
Author: tanmaytaneja2
Given the low constraints $$$(N ≤ 1000)$$$, a direct simulation of the contest process is an efficient solution. We maintain counts of the slices eaten by Tanmay and Rahul, and they take turns according to the specified pattern. The winner is determined by comparing the total slices consumed by Tanmay and Rahul. In case they consume an equal number of slices, the result is a draw.
void solve()
{
// main code
ll n;
cin >> n;
ll tanmay = 0, rahul = 0;
ll x = 1;
while(n){
ll to_add = min(n, x);
if(x % 2) tanmay += to_add;
else rahul += to_add;
n -= to_add;
x++;
}
if(tanmay > rahul){
cout << "Tanmay";
} else if(rahul > tanmay){
cout << "Rahul";
} else {
cout << "Draw";
}
return ;
}
Try to solve it in $$$log(n)$$$ time.
Author: Gaurav3478
Let us first solve the problem assuming that only one of the two robots can move. To solve this subproblem, it is important to note that diagonal moves are possible. For example, if the robot has to move $$$5$$$ steps to the left and $$$6$$$ steps downwards, then it is optimal to move $$$5$$$ steps diagonally (down and left) and one step down. Thus, it is clear that the total number of moves can be broken up into two parts: The diagonal moves and the non diagonal moves. Let $$$x$$$ be the horizontal distance between the two robots, and $$$y$$$ be the vertical distance. Thus, the total number of steps that a robot has to move to reach the other robot will be: $$$min(x, y) + (max(x, y) - min(x, y))$$$. Here, the first term corresponds to the diagonal moves, and the remaining terms corresponds to the non-diagonal moves. This simplifies to be $$$max(x, y)$$$. Now coming back to the orginal problem, where both robots can move. The number of steps will simply be $$$max(x, y) / 2$$$ if $$$max(x, y)$$$ is even, or $$$max(x, y) / 2 + 1$$$ if $$$max(x, y)$$$ is odd. This can be simply expressed as $$$(max(x, y) + 1) / 2$$$.
int main() {
int n, m, r1, c1, r2, c2;
cin >> n >> m >> r1 >> c1 >> r2 >> c2;
int dist = max(abs(r1 - r2), abs(c1 - c2));
cout << (dist + 1) / 2;
}
Author: harshsingla06
While the problem may appear complex at first glance, it can be efficiently solved using a straightforward greedy method.
Both gangs have a common objective: maximizing their power. The key to achieving this lies in acquiring weapons with the highest strength. To do this, we simply sort the weapons based on their strength in descending order. As we iterate through the sorted weapons list, we allocate them to the nearest gang that still has available members. It's worth noting that if one gang is closer to a weapon but has exhausted its members, the other gang can rightfully claim it. The complexity is $$$n \cdot m \cdot \log{(n \cdot m)}$$$.
import java.util.*;
public class Solution{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt(); int m = sc.nextInt();
ArrayList<int []> weapons= new ArrayList<>();
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
weapons.add(new int[] {sc.nextInt(), i, j});
}
}
Collections.sort(weapons, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o2[0], o1[0]);
}
});
int x1= sc.nextInt(); int y1= sc.nextInt();
int x2= sc.nextInt(); int y2= sc.nextInt();
int k1= sc.nextInt(); int k2= sc.nextInt();
long ans=0;
for (int i=0; i<n*m && (k1>0 || k2>0); i++){
int arr[]=weapons.get(i);
int dist1= Math.abs(arr[1]-x1)+Math.abs(arr[2]-y1);
int dist2= Math.abs(arr[1]-x2)+Math.abs(arr[2]-y2);
if (k1!=0 && (k2==0 || dist1<dist2)){
ans+=arr[0];
k1--;
}
else {
ans-=arr[0];
k2--;
}
}
if (ans>0) System.out.println(1);
else if (ans<0) System.out.println(2);
else System.out.println(0);
}
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve()
{
int n, m;
cin >> n >> m;
vector<vector<int>> v(n, vector<int>(m, 0));
for (auto &e : v)
for (auto &c : e)
cin >> c;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int k1, k2;
cin >> k1 >> k2;
vector<array<int, 3>> vals;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (v[i][j] > 0) {
vals.push_back({ v[i][j], i, j });
}
}
}
sort(begin(vals), end(vals), [] (auto &e1, auto &e2) {
return e1 > e2;
});
long long ballas = 0, bloods = 0;
for (auto &e : vals) {
// cout << e[0] << ' ' << e[1] << ' ' << e[2] << '\n';
if (k1 <= 0 and k2 <= 0) {
break;
}
if (k1 <= 0) {
k2--;
bloods += e[0];
continue;
}
if (k2 <= 0) {
k1--;
ballas += e[0];
continue;
}
int d1 = abs(e[1] - x1) + abs(e[2] - y1);
int d2 = abs(e[1] - x2) + abs(e[2] - y2);
if (d1 < d2) {
k1--;
ballas += e[0];
}
else {
k2--;
bloods += e[0];
}
// cout << ballas << ' ' << bloods << '\n';
// cout << k1 << ' ' << k2 << '\n';
}
if (ballas > bloods) {
cout << "1\n";
}
else if (ballas < bloods) {
cout << "2\n";
}
else {
cout << "0\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Author: prathamtyagii
When we are looking at the bookshelf from the right hand side we will never see a book if a book of greater or equal size is already there in the right subarray.
Consider $$$l$$$ to be the length of the increasing subsequence of heights, then we can break the problem into $$$2$$$ cases:
When the subsequence doesn't end at the $$$0^{th}$$$ index, then he can add books of greater height than the last book of the subsequence and the answer will be $$$(l+k)$$$ books.
When the subsequence ends at the $$$0^{th}$$$ index:
2.1. When the sum of books that can be inserted between consecutive books of the subsequence is $$$\ge k$$$, the answer is $$$(l+k)$$$ books
2.2 When the sum of books that can be inserted between consecutive books of the subsequence is $$$\lt k$$$, then he can sacrifice the last book of the subsequence and add books of height greater than the second largest book of the subsequence, the answer is $$$(l+k-1)$$$ books.
Time complexity: $$$\mathcal{O}(n)$$$
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int t = Integer.parseInt(line.split(" ")[0]);
while(t>0){
t--;
String line1 = br.readLine();
int n = Integer.parseInt(line1.split(" ")[0]);
long k = Long.parseLong(line1.split(" ")[1]);
int[] arr = new int[n];
String str = br.readLine();
String[] items = str.split(" ");
for(int j = 0; j < n; j++){
arr[j]=Integer.parseInt(items[j]);
}
if(n==1){
System.out.println(1);
continue;
}
int prevGreatest=arr[n-1];
long len=1;
long totaldifferences=0;
for(int i=n-2;i>=0;i--){
if(arr[i]>prevGreatest){
totaldifferences+=(long)(arr[i]-prevGreatest-1);
prevGreatest=arr[i];
len++;
}
}
boolean flag=true;
for(int i=1;i<n;i++){
if(arr[i]==prevGreatest){
flag=false;
break;
}
}
if(!flag){
len+=k;
}else{
if(totaldifferences>=k){
len+=k;
}else{
len+=k-1;
}
}
System.out.println(len);
}
}
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=0;
cin>>t;
while(t--){
long long n,k,x=0,mx_idx=0,mx=INT_MIN,count=0,maxi=0;
cin >>n>>k;
vector<long long> h;
for(int i=0;i<n;i++){
cin>>x;
if(mx<x){mx=x;mx_idx=i;}
h.push_back(x);
}
if(n==1){cout<<1<<endl;continue;}
for(int i=n-1;i>=0;i--){
if(h[i]>maxi){count++;maxi=h[i];}
}
long long p=std::count(h.begin(),h.end(),mx);
if(h[0]==mx && p==1){
cout<<max(count+k-1,count+min(k,mx-h[n-1]+1-count))<<endl;
}
else{cout<<count+k<<endl;}
}
return 0;
}
Author: vrintle
It is obvious that there are $$$n^2$$$ possible points available i.e., all $$$(x_i, y_j)$$$ pairs. And, we can observe that with the increase in radius, the number of points will either increase or remain the same, so the function (number of points inside) is monotonous with the radius. Hence, we can do a binary search on the radius.
Now, we just have to calculate the amount of points inside the circle for a given radius. Let's observe the function: $$$x^2 + y^2 = r^2$$$. In this, as $$$x$$$ increases, $$$y$$$ decreases. So, if we sort the arrays by squared values, then we can use two pointers to calculate the count, which takes linear time.
Time complexity: $$$\mathcal{O}(n \cdot \log{R} + n \cdot \log{n})$$$
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18
#define ld long double
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
const int N = 1e6 + 7;
int x[N], y[N];
void solve() {
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++) {
cin >> x[i];
}
for(int i = 0; i < n; i++) {
cin >> y[i];
}
sort(x, x + n, [&](auto& x, auto& y) {
return abs(x) < abs(y);
});
sort(y, y + n, [&](auto& x, auto& y) {
return abs(x) < abs(y);
});
ld low = 0, high = 2e9, eps = 1e-9;
auto check = [&](ld r) {
int j = n - 1, ans = 0;
for(int i = 0; i < n; i++) {
while(j >= 0 && r * r - x[i] * x[i] - y[j] * y[j] < eps * eps) {
j--;
}
ans += j + 1;
}
return ans >= k;
};
while(high - low > eps) {
ld mid = (high + low) / 2;
if(check(mid)) high = mid;
else low = mid;
}
ld ans = check(low) ? low : high;
cout << fixed << setprecision(9) << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef VRINTLE
freopen("./tests/test_0.txt", "r", stdin);
freopen("./out.txt", "w", stdout);
freopen("./err.txt", "w", stderr);
#endif
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
}
return 0;
}




