So I wrote this loop to answer a question in a recent contest by CodeChef, and I've failed to attain the best solution to it, so I figured this might be the best place to ask this question:-
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int x,y,n,r;
cin>>x>>y>>n>>r;
int maxx = INT_MIN;
int maxy = INT_MIN;
if(x*n > r){
cout<< "-1"<<"\n";
}
else{
int i=0;
do{
maxy = max(maxy, i);
maxx = n - maxy;
i++;
}
while((n-i)*x + i*y <= r && (n-i)>=0);
cout<< maxx <<" "<<maxy<<"\n";
}
}
return 0;
}
Related
https://cses.fi/problemset/task/1649
I'm solving this problem using Segment Trees and the solution I've written is
#include <bits/stdc++.h>
#define MAX 1000000001
using namespace std;
int n;
vector<int> tree;
int sum(int a, int b)
{
a += n;
b += n;
int s = INT_MAX;
while(a <= b) {
if (a % 2 == 1) s = min(s, tree[a++]);
if (b % 2 == 0) s = min(s, tree[b--]);
a>>=1;
b>>=1;
}
return s;
}
void update(int k, int change)
{
k += n;
tree[k] = change;
for(int i = k>>1; i >= 1; i>>=1) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
return;
}
int main()
{
int q;
cin >> n >> q;
n = pow(2, ceil(log2(n)));
tree.resize(2*n, INT_MAX);
for(int i = 0; i < n; i++) {
cin >> tree[i+n];
}
for(int i = n-1; i >= 1; i--) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
int type, a, b;
for(int i = 0; i < q; i++) {
cin >> type >> a >> b;
if (type == 1) {
update(a-1, b);
} else {
cout << sum(a-1, b-1) << endl;
}
}
return 0;
}
It works with first test case, but not with the second one. I've looked at other solutions online and they all look similar to mine. Please, help me spot the mistake.
This is a very simple program where the user inputs (x,y) coordinates and distance 'd' and the program has to find out the number of unrepeated coordinates from (x,y) to (x+d,y).
For eg: if input for one test case is: 4,9,2 then the unrepeated coordinates are (4,9),(5,9) and (6,9)(x=4,y=9,d=2). I have used a sorting algorithm as mentioned in the question (to keep track of multiple occurrences) however the program shows runtime error for test cases beyond 30. Is there any mistake in the code or is it an issue with my compiler?
For a detailed explanation of question: https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/algorithm/missing-soldiers-december-easy-easy/
#include <stdio.h>
#include <stdlib.h>
int partition(int *arr, int p, int r) {
int x;
x = arr[r];
int tmp;
int i = p - 1;
for (int j = p; j <= r - 1; ++j) {
if (arr[j] <= x) {
i = i + 1;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[i + 1];
arr[i + 1] = arr[r];
arr[r] = tmp;
return (i + 1);
}
void quicksort(int *arr, int p, int r) {
int q;
if (p < r) {
q = partition(arr, p, r);
quicksort(arr, p, q - 1);
quicksort(arr, q + 1, r);
}
}
int count(int A[],int ct) {
int cnt = 0;
for (int i = 0; i < ct; ++i) {
if (A[i] != A[i + 1]) {
cnt++;
}
}
return cnt;
}
int main() {
int t;
scanf("%d", &t);
long int tmp, y, d;
int ct = 0;
int i = 0;
int x[1000];
int j = 0;
for (int l = 0; l < t; ++l) {
scanf("%d%d%d", &tmp, &y, &d);
ct = ct + d + 1; //this counts the total no of coordinates for each (x,y,d)
for (int i = 0; i <= d; ++i) {
x[j] = tmp + i; //storing all possible the x and x+d coordinates
j++;
}
}
int cnt;
int p = ct - 1;
quicksort(x, 0, p); //quicksort sorting
for (int l = 0; l < ct; ++l) {
printf("%d ", x[l]); //prints sorted array not necessary to question
}
cnt = count(x, ct); //counts the number of non-repeated vertices
printf("%d\n", cnt);
}
The problem was the bounds of the array int x[1000] is not enough for the data given below.
Can anybody please explain the calculation of time and space complexity of this code(finding determinant of a matrix) in detail?
I am not able to understand the exact time complexity of this program using recurrence tree.
class Solution
{
static void getCofactor(int arr[][], int temp[][], int n, int i, int j){
int x = 0, y = 0;
for(int row=0; row<n; row++){
for(int col=0; col<n; col++){
if(row!=i && col!=j){
temp[x][y++] = arr[row][col];
if(y == n-1){
y = 0;
x++;
}
}
}
}
}
static int determinantOfMatrix(int matrix[][], int n)
{
// code here
if(n == 1)
return matrix[0][0];
int temp[][] = new int[n-1][n-1];
int determinant = 0;
int sign = 1;
for(int i=0; i<n; i++){
getCofactor(matrix, temp, n, 0, i);
determinant+=sign*matrix[0][i]*determinantOfMatrix(temp, n-1);
sign=-sign;
}
return determinant;
}
}
I was doing this problem on SPOJ. www.spoj.com/problems/TIP1.
I have written this code but I am getting time limit exceeded when judged. Can anyone help me with any optimization or a better approach.
if N is a positive integer, then PHI(N) is the number of integers K for which GCD(N, K) = 1 and 1 ≤ K ≤ N. We denote GCD the Greatest Common Divisor. For example, we have PHI(9)=6.
#include<iostream>
#include<vector>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 10000010
#define MAXN 10000010
int phi[MAXN + 1], prime[MAXN/10], sz=0;
vector<bool> mark(MAXN + 1);
int ans[10000011];
vector<int> a(10);
vector<int> b(10);
bool isprm(long int x)
{
for(int s=0; s<10; s++)
{
a[s]=b[s]=0;
}
long int y=phi[x];
int i=0,j=0;
while(x>0)
{
int rem=x%10;
x=x/10;
a[i]=rem;
i++;
}
while(y>0)
{
int rem=y%10;
y=y/10;
b[j]=rem;
j++;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if(i!=j)
return false;
for(int s=0; s<10; s++)
{
if(a[s]!=b[s])
return false;
}
return true;
}
void precompute_again()
{
for(int i=0; i<=20; ++i)
ans[i]=0;
ans[21]=21;
for(long int i=22; i<10000005; ++i){
bool chk=false;
chk=isprm(i);
if(chk==true)
{
if(i*phi[ans[i-1]]==phi[i]*ans[i-1])
{
ans[i]=i;
}
else
{
if(i*phi[ans[i-1]]>phi[i]*ans[i-1])
{
ans[i]=ans[i-1];
}
else
{
ans[i]=i;
}
}
}
else
{
ans[i]=ans[i-1];
}
}
}
int main()
{
phi[1] = 1;
for (int i = 2; i <= MAXN; i++ ){
if(!mark[i]){
phi[i] = i-1;
prime[sz++]= i;
}
for (int j=0; j<sz && prime[j]*i <= MAXN; j++ ){
mark[prime[j]*i]=1;
if(i%prime[j]==0){
int ll = 0;int xx = i;
while(xx%prime[j]==0)
{
xx/=prime[j];
ll++;
}
int mm = 1;
for(int k=0;k<ll;k++)mm*=prime[j];
phi[i*prime[j]] = phi[xx]*mm*(prime[j]-1);
break;
}
else phi[i*prime[j]] = phi[i]*(prime[j]-1 );
}
}
precompute_again();
int t;
scanf("%d",&t);
while(t--)
{
long int m;
scanf("%ld",&m);
cout<<ans[m]<<endl;
}
return 0;
}
Try to use a variant of Sieve of Eratosthenes.
The following code computes all phi[N] up to MAXN. For MAXN = 1e7 it runs in the blink of an eye.
int i, j;
int * phi = new int [MAXN];
for (i = 1; i < MAXN; i ++)
phi[i] = i;
for (i = 2; i < MAXN; i ++)
{
if (phi[i] != i) continue;
for (j = i; j < MAXN; j += i)
phi[j] = phi[j] / i * (i - 1);
}
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void quicks(int *arr,int x,int pivot,int lo,int hi);
void swap1(int *x,int *y);
int main()
{
int *arr = new int[7];
arr[0] = 23;
arr[1] = 3;
arr[2] = -23;
arr[3] = 45;
arr[4] = 12;
arr[5] = 76;
arr[6] = -65;
quicks(arr,7,0,1,6);
for(int i = 0;i<7;i++)
std::cout << arr[i] <<"\t";
getch();
return 0;
}
void quicks(int *arr,int x,int pivot,int lo,int hi)
{
int i = lo,j = hi;
if(pivot < x-1)
{
while(i <= hi)
{
if(arr[i] <= arr[pivot])
i++;
else
break;
}
while(j >= lo)
{
if(arr[j] >= arr[pivot])
j--;
else
break;
}
if( i > j)
{
swap1(&arr[j],&arr[pivot]);
lo = pivot+1;
hi = x - 1;
quicks(arr,x,pivot,lo,hi);
}
else if(i == j)
{
pivot = pivot + 1;
lo = pivot+1;
hi = x - 1;
quicks(arr,x,pivot,lo,hi);
}
else if(i < j)
{
swap1(&arr[j],&arr[i]);
lo = i + 1;
hi = j - 1;
quicks(arr,x,pivot,lo,hi);
}
}
else
{
printf("\nDONE\n");
}
}
void swap1(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Hi,
I have written a program to implement Quick sort.But the program is going into an infinite loop.In the Quicks function,the while loops for incrementing and decrementing i and j are the problem.Can anybody tell me what is wrong with this Implementation of QUick Sort.
Do a quick dry run using your input and the algorithm, you'll notice you run into an infinite cycle after a while.
You are starting the loop from quicks(arr,7,0,1,6);. Try instead by setting low to the "rightmost" element, that is 0. This won't definitely solve your problem as the algorithm seems really flawed, with the position of high not changing at all, and i moving all the way to high. You're trying to complicate a very simple task.
i would go from lo to pivot. And j from hi to pivot. After the pivot is found to be in the right place, you'll move forward pivot and lo by 1, and repeat the process.
Take a look at this image, you'll get an idea of the algo required: