I'm currently practising the kickstart coding challenges by google. here is a link to see the question :
https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050e01/00000000000698d6
I have made the program which succeeded giving the right answer using the sample question which the question provided but when I actually submit it for checking, the code met a runtime error. Since it won't give why so, I have no idea how to proceed. So I hope you guys can help...
Here is the code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int ts;
cin >> ts;
for (int i = 1; i <= ts; ++i)
{
int n, p;
cin >> n >> p;
// input skill level and sort descending
int skill[100] = {0};
for (int i = 0; i < n; i++) cin >> skill[i];
int size = sizeof(skill) / sizeof(skill[0]);
sort(skill, skill + size, greater<int>());
int ans= 9999999;
for (int i = 0; i <=(n-p); i++)
{
int tmp = 0;
for (int j = i + 1; j <= i + (p - 1); j++)
tmp += skill[i] - skill[j];
ans = min(ans, tmp);
}
cout << "Case #" << i << ": " << ans << endl;
}
}
So the question says that N can be upto 10000, but your code assumes that it is no bigger than 100.
Regarding LTE, you are almost there. Try to replace the
for (int j = i + 1; j <= i + (p - 1); j++)
tmp += skill[i] - skill[j];
loop with the constant time expression. Hint: when the most skillful player leaves the window, by how much the training time for the rest players gets decreased?
Related
I recently started learning Dynamic Programming, and am currently trying to solve "Roller Coaster Fun" on Kattis. However, I'm only getting 10/30 test cases (wrong answer on test case 11), and am stuck on what I am doing wrong.
My thinking is very similar to the coin problem (7.1 in the CPH, scroll down to "Coin Problem"), where we find the answer for each of the 25,000 times using the previous ones:
(this is pseudocode)
// fun[i] is the maximum amount of fun possible at time i
// k[i][j] is the number of times Jimmy goes on coaster j at time i
// all other functions/variables are as given in the problem
fun[1] = 0
fun[n] = max(fun[n - 1], // last fun value (don't go on any new ride)
fun[n - t[0]] + f(0, k[n][0]),
...,
fun[n - t[i]] + f(i, k[n][i]),
...,
fun[n - t[N - 1]] + f(N - 1, k[n][N - 1]))
Here is my full code:
// https://open.kattis.com/problems/rollercoasterfun
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 100
#define MAX_T 25000
vector<int> a(MAX_N), b(MAX_N), t(MAX_N);
vector<int> dp(MAX_T + 1);
vector<vector<int>> k(MAX_T + 1, vector<int>(MAX_N));
int f(int time, int i)
{
return max(0, a[i] - k[time][i] * k[time][i] * b[i]);
}
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; ++i)
cin >> a[i] >> b[i] >> t[i];
dp[0] = 0;
fill(k[0].begin(), k[0].end(), 0);
for (int time = 1; time <= MAX_T; ++time)
{
dp[time] = dp[time - 1];
k[time] = k[time - 1];
for (int i = 0; i < N; ++i)
{
if (time - t[i] >= 0)
{
int newFun = dp[time - t[i]] + f(time - t[i], i);
if (newFun > dp[time])
{
dp[time] = newFun;
k[time] = k[time - t[i]];
++k[time][i]; // go on this ride one more time
}
}
}
// cout << time << ' ' << dp[time] << '\n';
}
int Q;
cin >> Q;
int q;
for (int i = 0; i < Q; ++i)
{
cin >> q;
cout << dp[q] << '\n';
}
return 0;
}
Any help would be appreciated!
So, I am trying to write a program to compute the value of the series 1/0! + 1/1! + 1/2! + .. + 1/n!. I think this is a pretty easy question to solve. But, the 1/0! i.e. the 1st term of the series is where it is creating all the problems for me. Please help me out. Please pardon for the silly mistakes if I have made since I started learning C++ 2 months ago and I am trying to solve various problems ever since.
#include <iostream>
using namespace std;
int fact(int j)
{
int facto = 1;
if (j == 0) {
return facto;
}
else {
for (int i = 2; i <= j; i++) {
facto = facto * i;
}
return facto;
}
}
int main()
{
int n, p;
float sum = 0, k;
cout << "Enter the value of n: " << endl;
cin >> n;
for (int i = 0; i < n; i++) {
p = fact(i);
cout << p << endl;
k = 1 / p;
cout << k << endl;
sum = sum + k;
}
cout << "Sum is: " << sum << endl;
return 0;
}
I have 2 questions :
1) When I am giving 0 as input, I am getting 0(i.e. the value of sum) as output(but, I am expecting to get 1) and, when I am giving 1 as input, it is giving 1 as output(expecting 2 as output). Please help me in pointing out the loophole.
2) When I am giving 0 as input, it is not printing the values of 'p' and 'k' but any input greater than 0 is showing the values of 'p' and 'k' for each time it completes the loop. Why ??
Change the loop the following way
for(int i=0; i<=n; i++)
^^^^^
Or change the loop to do-while loop as for example
int i = 0;
do
{
//...
} while ( i++ < n );
Also this statement
k = 1/p ;
change like
k = 1.0f/p ;
My for loop stops at the 88th number which I find very odd. I cannot figure out why it is doing this. It doesnt exit the for loop and it doesnt continue reading any numbers. Im very confused.
#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
double distanceCalc(double x1, double y1, double x2, double y2)
{
return sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));
}
int main()
{
int n;
double x, y, min = 10000000;
vector<double> xCoor;
vector<double> yCoor;
while (cin >> n && n != 0)
{
int numPairs = n;
cout << numPairs << endl;
for (int i = 0; i < numPairs; i++)
{
cin >> x >> y;
cout << x << " " << y << " " << i << endl;
xCoor.push_back(x);
yCoor.push_back(y);
}
if (n == 1)
{
min = 1000000000;
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
double temp = distanceCalc(xCoor[i], yCoor[i], xCoor[j], yCoor[j]);
if (temp < min)
{
min = temp;
}
}
}
(min < 10000) ? printf("%.4f\n", min) : printf("INFINITY\n");
}
return 0;
}
the input im using is: https://drive.google.com/open?id=1SNZAVh2lkiih-RSQRa9nH5Oj6RD-fw5Z
Both I and others have tried your code, and have not been able to reproduce the problem.
It is of course possible that there is something wrong with your C++ installation, but a more common problem when these things happen is that you are running an older version of the program than the one you think you are running. So check carefully that you are actually re-compiling the code after each change, and that you are really using the source file that you think. You could, for example, add an extra printout to the program, and see if you find it in the output.
Also check that you are using the input file you think you are using. Perhaps you are really reading another version of the input, and that one has some garbage after the first 88 numbers?
Would like to seek a bit of help from StackOverflow. I am trying to print out the sequence of Fibonacci number and also the number of time the iterative function is called which is supposed to be 5 if the input is 5.
However, I am only getting 4199371 as a count which is a huge number and I am trying to solve the problem since four hours. Hope anyone who could spot some mistake could give a hint.
#include <iostream>
using namespace std;
int fibIterative(int);
int main()
{
int num, c1;
cout << "Please enter the number of term of fibonacci number to be displayed: ";
cin >> num;
for (int x = 0; x <= num; x++)
{
cout << fibIterative(x);
if (fibIterative(x) != 0) {
c1++;
}
}
cout << endl << "Number of time the iterative function is called: " << c1 << endl;
}
int fibIterative(int n)
{
int i = 1;
int j = 0;
for(int k = 1; k <= n; k++) {
j = i + j;
i = j - i;
}
return j;
}
First, initialize the variable
c1 = 0;
so that you will not get any garbage value get printed.
Secondly this:
if (fibIterative(x) != 0)
{
c1++;
}
will make 2*count - 1 your count. You don't need that.
Edit: I have noticed that you have removed extra c1++; from your first revision. Hence, the above problem is not more valid. However, you are calling the function fibIterative() again to have a check, which is not a good idea. You could have simply print c1-1 at the end, to show the count.
Thirdly,
for (int x = 0; x <= num; x++)
you are starting from 0 till equal to x that means 0,1,2,3,4,5 total of 6 iterations; not 5.
If you meant to start from x = 1, you need this:
for (int x = 1; x <= num; x++)
{ ^
cout << fibIterative(x) << " ";
c1++;
}
Currently studying software engineering at college (first year) and made a program where the user enters how many entries there will be and then they input the times for each entry and it is sorted in descending order.
The problem I am having is when I enter a large number for the first input it doesn't sort correctly but the rest do. It would be great if someone could help me out with this and sorry for the noob question:P
The entire code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int TotalSize = 0;
void getSpeed(int *CalculationTime, int NoOfCalculations)
{
for (int i = 0; i < NoOfCalculations; i++)
{
cout << "Please enter the speed of calculation " << i + 1 << "(Ms): "; cin >> CalculationTime[i];
}
}
void sort_speeds(int *CalculationTime, int NoOfCalculations)
{
// Sorting speeds in decending order
bool swapped = true;
int i, j = 0;
int temp;
while (swapped)
{
swapped = false;
j++;
for (i = 1; i < NoOfCalculations - j; i++)
{
if (CalculationTime[i] > CalculationTime[i + 1])
{
temp = CalculationTime[i];
CalculationTime[i] = CalculationTime[i + 1];
CalculationTime[i + 1] = temp;
swapped = true;
}
}
}
// Output times decending order
for (int i = 0; i < NoOfCalculations; i++)
{
cout << CalculationTime[i] << "\n";
}
}
int main()
{
// Declaring & Initializing variables
int NoOfCalculations = 0;
int *CalculationTime = new int[NoOfCalculations];
// Getting user input
cout << "How many calculations are there? "; cin >> NoOfCalculations;
getSpeed(CalculationTime, NoOfCalculations);
// Sorting and displaying times
sort_speeds(CalculationTime, NoOfCalculations);
system("pause");
return 0;
}
You've never compare first element of your array with anything - for (i = 1; i < NoOfCalculations - j; i++) should be for (i = 0; i < NoOfCalculations - j; i++)
The issue is for (i = 1; i < NoOfCalculations - j; i++) You are starting at position 1, start at position 0 and it fixes the problem. for (i = 0; i < NoOfCalculations - j; i++)
// Declaring & Initializing variables
int NoOfCalculations = 0;
int *CalculationTime = new int[NoOfCalculations];
// Getting user input
cout << "How many calculations are there? "; cin >> NoOfCalculations;
Bzzzt. You allocate a zero-element array, and then don't reallocate it. I bet if you entered a large enough number for your number of calculations, your program would crash.
Really, you want to be using std::vector, an extremely useful datastructure, the use of which is a bit outside of the scope of this answer. Basically, you can do stuff like this:
std::vector<int> getSpeeds(int NoOfCalculations)
{
std::vector<int> speeds;
for (int i = 0; i < NoOfCalculations; i++)
{
int speed;
std::cout << "Please enter the speed of calculation " << i + 1 << "(Ms): ";
std::cin >> speed;
speeds.push_back(speed);
}
return speeds;
}
You can use the returned vector almost exactly as if it were an array:
std::vector<int> speeds = getSpeeds(10);;
if (CalculationTime[3] > CalculationTime[4])
// do something
Often, in a C++ application, the explicit use of pointers is a sign that you're not using the standard library, and as a result making life much, much harder for yourself.
Oh, and also:
for (i = 1; i < NoOfCalculations - j; i++)
You never look at NoOfCalculations[0] or NoOfCalculations[i - 1], so you never touch the first element.
while (swapped)
{
swapped = false;
j++;
for (i = 0; i < NoOfCalculations - j; i++) //try and start i from 0. I think you are missing the first element to check
{
if (CalculationTime[i] > CalculationTime[i + 1])
{
temp = CalculationTime[i];
CalculationTime[i] = CalculationTime[i + 1];
CalculationTime[i + 1] = temp;
swapped = true;
}
}
}