You are going to receive payments in the upcoming month. The payments are numbered from to and denotes the amount of money in dollars that will be paid in connection with the i-th payment. Before receiving the payments, you have two banking options to consider:
You can receive all the payments on your current bank account remembering that the bank charges you for each payment i max(k,x% of p_i ) , where k and x are given.
You can pay the bank d dollars upfront to open a new special account for which the bank doesn't charge you for any of the upcoming transactions.
Your task is to decide which of the above two options is more profitable to you. If both ways are equally profitable, then you prefer to be charged for each transaction.
Input Format
In the first line, there is a single integer,q denoting the number of scenarios to handle. After that, descriptions of all these scenarios follow. In the first line of a single scenario, there are space-separated integers . In the second line, there are space separated integers p_0,p_1,p_2,...p_(n-1).
Constraints:
1<=q<=5
1<=n<=100
1<=p_i<=1000
1<=k<=1000
1<=x<=100
1<=d<=100000
print fee if it's more profitable to use the current account and let the bank charge for each transaction, or print upfront if it's better to pay the bank up front for opening the new special account.
Below is my code: I am not sure why it does not cover all the cases, please enlighten me on this, thank you!
string feeOrUpfront(int n, int k, int x, long int d, vector <int> p)
{
double sum=0;
double amountcharged;
amountcharged=0;
for (int i=0;i<n;i++)
{
if ((p[i]*x/100)>k)
{amountcharged=p[i]*x/100;}
else
{amountcharged=k;}
sum=sum+amountcharged;
}
if (sum>d)
{
return "upfront";
}
else
{
return "fee";
}
}
int main() {
int q;
cin >> q;
for(int a0 = 0; a0 < q; a0++){
int n;
int k;
int x;
int d;
cin >> n >> k >> x >> d;
vector<int> p(n);
for(int p_i = 0; p_i < n; p_i++){
cin >> p[p_i];
}
string result = feeOrUpfront(n, k, x, d, p);
cout << result << endl;
}
return 0;
}
p[i]*x/100) is calculated multiple times and stored as double. Probably you want to calculate p[i]*x/100.0 as double.
In main the vector<int> p you can push the values to append them. So you don't need to access the container with the indexer.
Also you might avoid the for (int i loop in feeOrUpfront with an iterator and std::for_each.
IMHO Your question would match better at https://codereview.stackexchange.com/
Related
I'm using a sequence of x numbers and search the lower and higher number of it and calculate the diff, but when I use a variable to save the highest number it don't save, I don't have that problem with the lower variable.
for(int i=0;i<mesos;i++){
cin >>m;
if(m<e) a = m;
else if(m<e) e = m;
all is declared
Just for elegance as this is a C++ question.
uint64_t min, max;
for(decltype(mesos) i = 0; i < mesos; ++i) {
std::cin >> m;
min = std::min(min, m);
max = std::max(max, m);
}
To save the highest number you have to test if the number is greater than another number. In your case you're calculating against the same value, the same thing. I didn't test my resolution but it would be something like what I wrote bellow. I used an if in the first time the loop runs so the highest and the lowest value are equal your input.
But test also gonewiththewhind answer, and see which is best for you.
Tip: use good variable names
int lower,higher;
for(int i=0;i<mesos;i++){
cin >> m;
if(i==0){
lower = m;
higher = m;
}
if(m>higher) higher = m;
else if(m<lower) lower = m;
}
I'm attempting a problem on codeforces. The gist of the question: There are n days, and on each day there are k products up for sale and l customers visiting. Each customer will either purchase one product or if there aren't any left, leave without buying anything. Any excess unpurchased product is thrown away. In order to maximise the number of products sold, you choose any f days from n and double the number of products put up for sale. Find the maximal number of products you can sell.
So my approach was to calculate min(2k, l) - min(k, l) for each of the days and store it in a vector v. I sort this vector and sum up the largest f of these values. Then, I sum up min(k, l) for every single day, and adding it to the previous sum I get my answer.
The problem is: When I do sort(v.rbegin(), v.rend()) and add the first f elements, I get the right answer. However, when I do sort(v.begin(), v.end()) and add the elements from index int i = v.size() - 1; i > v.size() - 1 - f; i-- aka the last f elements, I fail on the 28th test case.
The ranges are 1 ≤ n ≤ 10^5, 0 ≤ f ≤ n and 0 ≤ k i, l i ≤ 10^9. I'm not sure if it's a case of overflow though I doubt it because I used long long to store my answer, and on the test case I failed the expected output is a small integer. Thanks for reading, hope someone could help me out on this
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, f;
cin >> n >> f;
vector<int> v;
vector<int> v2;
for(int i = 0; i < n; i++)
{
int k, l;
cin >> k >> l;
int difference = min(2*k, l) - min(k, l);
v.push_back(difference);
v2.push_back(min(k, l));
}
sort(v.begin(), v.end());
long long ans = 0;
for(int i = v.size()-1; i > v.size()-1-f; i--)
{
ans += v[i];
}
for(int i = 0; i < v2.size(); i++)
{
ans += v2[i];
}
cout << ans << endl;
}
I will start by saying that I'm not a native speaker so please excuse me my grammatical errors.
I'm an university student and my task is the following: I have an input that tells me the number of people, and then every line contains the time of arrival and the time of exit, both natural numbers separated by a space.
I have to find the (index of the) person who met the most people and then output the number of meetings that person had.
Example input and output:
If person A has datestamps of 3 and 6 and person B has 6 and 7, it is still considered a meeting.
I already solved this problem with a fixed size array of structs that compares every person to everybody else to find out the number of meetings and then searched for the person with the most meetings.
My problem is that this code is very slow and I must hadle inputs consisting of maximum 200000 people and timestamps ranging from 1 to 1000000.
This - compare everyone with everyone else - solution works for small sample sizes, but there is no way it can work for 200000 structs.
Also, this code has to successfully run under 0.2 sec.
What is a faster way to solve this?
#include <iostream>
using namespace std;
const int maxN = 20000;
struct Data {
int arrival;
int departure;
int meetings = -1;
};
int main()
{
Data x[maxN];
int N;
///input
cin >> N;
for (int i = 0; i < N; i++) {
cin >> x[i].arrival;
cin >> x[i].departure;
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++){
if ( ((x[i].arrival >= x[j].arrival && x[i].arrival <= x[j].departure) || (x[i].departure >= x[j].arrival && x[i].departure <= x[j].departure)) || ((x[j].arrival >= x[i].arrival && x[j].arrival <= x[i].departure) || (x[j].departure >= x[i].arrival && x[j].departure <= x[i].departure)) ) {
x[i].meetings++;
}
}
}
int maxInd = 0;
int maximum = 0;
for(int i = 0; i < N; i++) {
if (x[i].meetings > maximum){
maxInd = i;
maximum = x[i].meetings;
}
}
///output
cout << maxInd+1 << endl;
cout << maximum << endl;
return 0;
}
I will only give you a starting point...
If I had to solve it, i would start by defining the following structure:
struct come_or_go {
size_t person_index;
int time;
bool arrival; // true for arrival, false for leaving
};
Next I would read the input into a vector<come_or_go> with two entries for each person. One when it arrives and one when it leaves. Next I'd sort that vector with respect to the elements time member. Finally I'd try to come up with a clever idea that requires to traverse this vector only once.
So far thats all I can provide, maybe I will update when I can give more hints. Hope this helps to push you into a differernt direction, because your brute force simply looses by complexity. Instead of trying to get details of it "faster" you need to change your overall approach.
I managed to do it by creating a vector and storing the 100.000 points of time in it.
I added 1 to each index where a person came in, stayed, and left.
Working with 100.000 long vectors, I managed to solve this problem with only one for loop se it ran reasonably fast.
Im trying to write a console application in c++ that will let the user input a series of numbers and the program should get the sum of all numbers, the average number, the largest and the second largest number.
For example:
Enter a couple of numbers : 10 12 -5 20 -2 15 0
Sum = 50
Average value = 8.3
Largest number = 20
Second largest = 15
#include<iostream>
#include<conio.h>
using namespace std;
int main( )
{
int a[5];
cout << "We are going to find the max value"<< endl;
int x;
for (x=0; x<5; x++)
{
cout<<"insert values"<<x+1<<endl;
cin>>a[x];
}
int max;
int min;
max = a[0];
min = a[0];
int e=0;
while (e<5)
{
if (a[e]>max)
{
max = a[e];
}
e++;
}
cout<<"Max value in the array is.."<<max<<endl;
getch();
return 0;
}
This is my progress so far.
Although, I have some concerns.
How do I let the user input the numbers like in the example and store them in a array with unknown size?
I'll try to figure out a way to count the avg, sum and second largest while waiting for this answer :)
Thanks!
To input an unknown number of elements, you use std::vector, inputting
until the user tells you to stop, typically by inputting an end of file:
std::vector<int> values;
int i;
while ( std::cin >> i ) {
values.push_back( i ) ;
}
If you're looking for some other type a signal for the end, you'll
probably have to read line by line, checking whether the line contains
your end criteron, and then use std::istringstream to parse the
integer.
For the rest: it may not correspond to the goal of the exercise, but
the standard library has a couple of functions which could make things
significantly simpler: std::max_element, for example, or
std::accumulate. And <conio.h> is not very portable, and is
deprecated on the systems which do support it.
If you can't use std::vector, you might want to learn about dynamic memory allocation.
int *a = new int[size];
this is my first time posting here. I'm working on a lab for my CS II class (it's already past due, so I won't get any points for it at this point, but I'd still really like to understand what I'm doing wrong so that I can improve. The code is nowhere near complete, but I've hit a hangup that's keeping me from proceeding. Apologies in advance, I'm very rusty at this, and before I was rusty, I was very new. Also, I was up 'til about 4 in the morning trying to get this to work last night, so my code is probably pretty gnarly-looking at the moment.
Okay, so my issue is this: while single-stepping the code, the first function, getNoMonths, runs perfectly fine. It prompts the user for the number of months of sales they'd like to process the data for, and stores the number in no_Months back in main. The validation for the value needing to be between 3 and 6 also works fine.
When we get to getSales, the problem arises. The function prompts for the sales values of however many months the user defined earlier. It seems like it works perfectly. Upon closer inspection, however, it seems that only the first value is actually being stored in sales, which is supposed to be an array, the size of which is dynamically allocated according to how many months the user defined during getNoMonths.
I'm sure that whatever I'm doing wrong is incredibly simple, I can feel it. I've been flipping through the text again, trying to learn as much as I can about how arrays and dynamic memory allocation and pointers all work. I get the ideas behind them, but as you can probably see, I'm really struggling with the actual implementation.
I know that what I NEED to do here is:
set up an array to hold sales data for each month
have the size of that array match the number of months as input by the user
...I'm just not sure how to actually do it. I also know I need to have a pointer in order for the second bullet item to happen, but again, I'm not really clear on what exactly I need to do.
TL;DR I need some help getting this sales array to hold ALL the values input by the user, not just the first one. To do this, I'll probably need help with setting up a pointer to dynamically allocate the size of the sales array, and preferably an explanation on how the hell all this actually helps me and isn't just confusing and redundant, lol.
Source code follows:
//Jesse Holland
//Lab1Out
//Processes Sales Data, Displays as a Bar Graph presentation
#include <iostream>
#include "graph1.h"
using namespace std;
//FUNCTION PROTOTYPES
int getNoMonths(int& no_months); //prompts for number of months of sales data to be processed
void getSales(int* sales, int no_months); //prompts for sales data, stores corresponding data in "sales"
int getMax(int* sales, int no_months, double & max); //maximum value for "sales" array
int getMin(int* sales, int no_months, double & min); //minimum value for "sales" array
double getAvg(int* sales, int no_months, double & avg); //computes avg of sales data stored in "sales" array
void displayResults(int* sales, int no_months, int max, int min, double avg); //displays bar chart diagram
//MAIN FUNCTION START
int main()
{
double max = 0;
double min = 0;
double avg = 0;
int no_months = 0;
//prompt for number of months
getNoMonths(no_months);
//variable declaration
int * sales = new int[no_months];
//prompt for sales data
getSales(sales, no_months);
//maximum value of "sales" array
getMax(sales, no_months, max);
//minimum value for "sales" array
getMin(sales, no_months, min);
//computes avg of sales data stored in "sales" array
getAvg(sales, no_months, avg);
//displays bar chart diagram in graphics window
displayResults(sales, no_months, max, min, avg);
return 0;
}
//FUNCTIONS IMPLEMENTATION START
int getNoMonths(int& no_months) // int* months)
{
do
{
cout << "Please enter the number of months you'd like to process: <must be at least 3, but no more than 6>:";
cin >> no_months;
//int* months = new int[no_months];
}while ((no_months < 3) || (no_months > 6));
return no_months;
}
void getSales(int* sales, int no_months)
{
int count = 0;
for (count = 0; count < no_months; count++)
{
cout << "Please enter sales for month #" << (count + 1) << ": ";
cin >> sales[count];
}
}
int getMax(int* sales, int no_months, double & max)
{
//int max = 0;
for(int count = 0; count < no_months, count++;)
{
if (sales[count] > max)
max = sales[count];
}
return max;
}
int getMin(int* sales, int no_months, double & min)
{
//int min = 0;
int count = 0;
for (count = 0; count < no_months; count++)
{
if (sales[count] < min)
min = sales[count];
}
return min;
}
double getAvg(int* sales, int no_months, double & avg)
{
int sum = 0;
for(int count = 0; count < no_months; count++)
sum += sales[count];
return avg = (sum/no_months);
}
void displayResults(int* sales, int no_months, int max, int min, double avg)
{
cout << "The maximum is " << max << ". \n";
cout << "The minimum is " << min << ". \n";
cout << "The average is " << avg << ". \n";
}
Thanks very, very much in advance for any and all help. I'm very, very green at all this, and although I'm trying very hard to get it, it seems impossible. I'm determined to learn how all this works and to be able to UNDERSTAND it so I can replicate it myself, but right now I need a little help getting there. It's hard not having any friends that no anything about this stuff and a professor who isn't terribly interested in sitting down with me and helping me work it out, either. Anyway, I look forward to hearing from you guys and hopefully learning some stuff. Maybe one day I can learn enough to be able to return the favor to newer people than I.
UPDATE: Updated source code; average is being properly calculated and output now, but still can't get max and min to do the same. Thanks for the help thus far, any further input will of course be appreciated.
Your main problem seems to be
int no_months = 0; followed by int * sales = new int [no_months]; which basically allocates an array of size 0. Try calling getNoMonths before allocating the array and fix return types.