Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
lets add some flavor of ingenuity to it.Addition operation requires cost now, and the cost is the summation of those two to be added. So,to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways1 + 2 = 3, cost = 31 + 3 = 4, cost = 42 + 3 = 5, cost = 53 + 3 = 6, cost = 62 + 4 = 6, cost = 61 + 5 = 6, cost = 6Total = 9Total = 10Total = 11I hope you have understood already your mission, to add a set of integers so that the cost is minimal.Input Each test case will start with a positive number,N(2N5000) followed by N positive integers(all are less than 100000). Input is terminated by a case where the value of N is zero. This case should not be processed.Output For each case print the minimum total cost of addition in a single line.
Sample Input
3
1 2 3
4
1 2 3 4
0
Sample Output
9
19
i tried to sort the given array and then took another array for cumsum (CS) and summed all element of CS except cs[0].. i am getting WA for this approach, please explain
int n,i,hold=0;
while(1)
{
cin>>n;
if(n==0){break;}
int arr[n],cs[n];
for(i=0;i<n;i++) cin>>arr[i];
sort(arr,arr+i);
cs[0]=arr[0];
for(i=1;i<n;i++){cs[i]=arr[i]+cs[i-1]; }
cs[0]=0;
int sum=0;
for(i=1;i<n;i++){sum+=cs[i]; }
cout<<sum<<endl;
sum=0;
}
input:
9
66 85 52 22 44 1 59 88 67
0
my out:
1822
expected result(udebug):
1454
getting WA
Your idea is wrong to solve this problem.
after taking all the elements on a data structure you should repeat this 3 points:
1)sort.
2)sum 1st two value,and remove 1st two value from the data structure
3)add the sum to the cost and data structure.
you can use priority_queue as the data structure.
Use min heap and add 2 smallest element. Example:
1 2 3 -> 3 3 -> 6.
1 2 3 4 -> 3 3 4 -> 4 6 -> 10.
Hope it helps.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
Improve this question
Could you pls. help with algorithm (I use C++) which seemed so simple at first glance.
I need a total enumeration of all combinations of N parameters to run some target function for all combination and choose one optimal combination.
Let say there are three parameters (it can be any number set by user but for this example let’s assume 3)
Possible values (can be any number for any parameter - NOT a fixed number - number of values set a user before program starts)
name
value
value
value
value
param1
1
2
..
param2
10
20
..
param3
100
200
300
..
So the number of combinations = 12 (2 * 2 * 3 = 12)
All combinations
#
param1
param2
param3
1
1
10
100
2
2
10
100
3
1
20
100
4
2
20
100
5
1
10
200
6
2
10
200
7
1
20
200
8
2
20
200
9
1
10
300
10
2
10
300
11
1
20
300
12
2
20
300
Ok – let say the order may be different
#
param1
param2
param3
1
1
10
100
2
1
20
100
3
1
10
200
4
1
20
200
5
1
10
300
6
1
20
300
7
2
10
100
8
2
20
100
9
2
10
200
10
2
20
200
11
2
10
300
12
2
20
300
However it’s obviously one counter should change while others fixed and after one counter finish increment next in the chain
This approach seems quite simple but I still can’t find an implementation. I thought use a list for the parameters and as one counter finish itereates it's values it call the next parameter and increment next parameter value and reset the first parameter counter. But how put it in couple of loops … ? I intuitively feels it should be quite simple solution.
The another approach I think – use all combinations to build a graph and after that traverse the whole graph and in the end get an optimal combination. But if I fill the graph it means I already solve this problem and building a graph just a waste of time and memory.
For now there is a sketch (in pseudo-code) like this:
std::list<param> params
bool isDone = false
func(node* n)
{
if(n->prev)
{
n->GetCurrentValue() // return current value and increment by one
n->prev->reset();
func(n->prev)
Run(); // target function
if(n->IsDone()) // finish end of the values
{
if(n->next)
func(n->next);
else
isDone = true;
}
}
else // first node in the list
{
while(n->IsDone()) // finish end of the values
{
n->GetCurrentValue() // return current value and increment by one
Run() // target function
}
n.reset() // set internal counter for the node to 0
func(n->next())
}
}
while(!isDone)
{
for(p : params)
{
func(p)
}
}
Please help me to understand the following code and what will be the possiable output.
What will be the output of the following pseudo code for input 7?
1.Input n
2.Set m = 1, T = 0
3.if (m > n)
Go to step 9
5.else
T = T + m
m = m + 1
8.Go to step 3
9.Print T
0
n is less than n so go to step 9 which is print T which is equal to 0 as set in step 2.
T should be 28. It will loop till m>7 (since n=7) and in each iteration T adds m to itself, since T is 0 initially it is only summing up m after incrementing it by 1 in each iteration.So if you add 1+2+3.....+7 you get 28 and that is when the loop breaks since m is now equal to 8.
for m = 1 2 3 4 5 6 7 and for 8 m>n will be true and it will go to step 9
T=(T+M)= 1 3 6 10 15 21 28 basically T is a series where next is added as 2,3,4,5,6,7 to prev number 2 3 4 5 6 7 if one look from other angle
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There is given array of size > n , we need to select n number of element from an array.
For example : array contain 112 element and n = 50, so select 50 number such that distance between every two selected number is more or less equal (equal distance is not possible of course except for size%n == 0 ).
If anyone suggest any idea that would work .
Example :
array = 1 2 3 4 5
n = 1
output : 1 or any another number depending on proposed algo.
n = 2
output : 1 3 or 2 4 or 1 4...
n = 3
output : 1 3 5
n = 4
output : 1 3 4 5 or 1 2 4 5
n = 5 :
output 1 2 3 4 5
Basically in case of n=1,2,4 there are more then one possible combination so I need to devise an algo which would pick numbers in uequally distributed manner.
One approach would be dividing the number of elements by the number of desired elements in the selection in floating point, and using rounding to determine the index:
double dist = ((double)size) / n;
int *res = new int[n];
for (int i = 0 ; i != n ; i++) {
res[i] = orig[round(dist*i)];
}
For your example of 112 and 50 the value of dist would be 2.24 and the sequence of indexes selected from the array would be
0 0
1 2
2 4
3 7
4 9
5 11
......
45 101
46 103
47 105
48 108
49 110
Is there efficient way to downscale number of elements in array by decimal factor?
I want to downsize elements from one array by certain factor.
Example:
If I have 10 elements and need to scale down by factor 2.
1 2 3 4 5 6 7 8 9 10
scaled to
1.5 3.5 5.5 7.5 9.5
Grouping 2 by 2 and use arithmetic mean.
My problem is what if I need to downsize array with 10 elements to 6 elements? In theory I should group 1.6 elements and find their arithmetic mean, but how to do that?
Before suggesting a solution, let's define "downsize" in a more formal way. I would suggest this definition:
Downsizing starts with an array a[N] and produces an array b[M] such that the following is true:
M <= N - otherwise it would be upsizing, not downsizing
SUM(b) = (M/N) * SUM(a) - The sum is reduced proportionally to the number of elements
Elements of a participate in computation of b in the order of their occurrence in a
Let's consider your example of downsizing 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 to six elements. The total for your array is 55, so the total for the new array would be (6/10)*55 = 33. We can achieve this total in two steps:
Walk the array a totaling its elements until we've reached the integer part of N/M fraction (it must be an improper fraction by rule 1 above)
Let's say that a[i] was the last element of a that we could take as a whole in the current iteration. Take the fraction of a[i+1] equal to the fractional part of N/M
Continue to the next number starting with the remaining fraction of a[i+1]
Once you are done, your array b would contain M numbers totaling to SUM(a). Walk the array once more, and scale the result by N/M.
Here is how it works with your example:
b[0] = a[0] + (2/3)*a[1] = 2.33333
b[1] = (1/3)*a[1] + a[2] + (1/3)*a[3] = 5
b[2] = (2/3)*a[3] + a[4] = 7.66666
b[3] = a[5] + (2/3)*a[6] = 10.6666
b[4] = (1/3)*a[6] + a[7] + (1/3)*a[8] = 13.3333
b[5] = (2/3)*a[8] + a[9] = 16
--------
Total = 55
Scaling down by 6/10 produces the final result:
1.4 3 4.6 6.4 8 9.6 (Total = 33)
Here is a simple implementation in C++:
double need = ((double)a.size()) / b.size();
double have = 0;
size_t pos = 0;
for (size_t i = 0 ; i != a.size() ; i++) {
if (need >= have+1) {
b[pos] += a[i];
have++;
} else {
double frac = (need-have); // frac is less than 1 because of the "if" condition
b[pos++] += frac * a[i]; // frac of a[i] goes to current element of b
have = 1 - frac;
b[pos] += have * a[i]; // (1-frac) of a[i] goes to the next position of b
}
}
for (size_t i = 0 ; i != b.size() ; i++) {
b[i] /= need;
}
Demo.
You will need to resort to some form of interpolation, as the number of elements to average isn't integer.
You can consider computing the prefix sum of the array, i.e.
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
yields by summation
0 1 2 3 4 5 6 7 8 9
1 3 6 10 15 21 28 36 45 55
Then perform linear interpolation to get the intermediate values that you are lacking, like at 0*, 10/6, 20/6, 30/5*, 40/6, 50/6, 60/6*. (Those with an asterisk are readily available).
0 1 10/6 2 3 20/6 4 5 6 40/6 7 8 50/6 9
1 3 15/3 6 10 35/3 15 21 28 100/3 36 45 145/3 55
Now you get fractional sums by subtracting values in pairs. The first average is
(15/3-1)/(10/6) = 12/5
I can't think of anything in the C++ library that will crank out something like this, all fully cooked and ready to go.
So you'll have to, pretty much, roll up your sleeves and go to work. At this point, the question of what's the "efficient" way of doing it boils down to its very basics. Which means:
1) Calculate how big the output array should be. Based on the description of the issue, you should be able to make that calculation even before looking at the values in the input array. You know the input array's size(), you can calculate the size() of the destination array.
2) So, you resize() the destination array up front. Now, you no longer need to worry about the time wasted in growing the size of the dynamic output array, incrementally, as you go through the input array, making your calculations.
3) So what's left is the actual work: iterating over the input array, and calculating the downsized values.
auto b=input_array.begin();
auto e=input_array.end();
auto p=output_array.begin();
Don't see many other options here, besides brute force iteration and calculations. Iterate from b to e, getting your samples, calculating each downsized value, and saving the resulting value into *p++.
I need help finding the formula of the sequence for the next problem.
What I think and have for now is Sn=n(10^n-1)/9 but it just works in some cases...
Here is the description of the problem:
Description
Sn is based upon the sequence positive integers numbers. The value n can be found n times, so the first 25 terms of this sequence are as follows:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7...
For this problem, you have to write a program that calculates the i-th term in the sequence. That is, determine Sn(i).
Input specification
Input may contain several test cases (but no more than 10^5). Each test case is given in a line of its own, and contains an integer i (1 <= i <= 2 * 10^9). Input ends with a test case in which i is 0, and this case must not be processed.
Output specification
For each test case in the input, you must print the value of Sn(i) in a single line.
Sample input
1
25
100
0
Sample output
1
7
14
Thanks solopilot! I made the code but the online judge show me Time Limit Exceeded, what could be my error?
#include <iostream> #include <math.h> using namespace std; int main() {int i;
int NTS;
cin>>i;
while (i>=1){
NTS=ceil((sqrt(8*i+1)-1)/2);
cout<<" "<<NTS<<endl;
cin>>i;
}
return 0;}
F(n) = ceiling((sqrt(8*n+1)-1)/2)
Say F(n) = a.
Then n ~= a * (a+1) / 2.
Rearranging: a^2 + a - 2n ~= 0.
Solving: a = F(n) = (-1 + sqrt(1+8n)) / 2.
Ignore the negative answer.
The pattern looks like a pyramid.
Level : 1 3 6 10 15 21 28...
No : 1 2 3 4 5 6 7...
Level = n(n+1)/2 => elements
3 = 3*4/2 => 6
6 = 6*7/2 => 21