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 years ago.
Improve this question
I have two workers.If I have configured a strategy that 60% tasks are allocate to A worker and the rest to B worker.
how do you implement with c++.
what is your suggestion?
map<string,float> m_percent;
m_percent.insert(make_pair("countA",0.6));
m_percent.insert(make_pair("countB",0.1));
m_percent.insert(make_pair("countC",0.3));
map<string,int> m_count;
m_count.insert(make_pair("total",0));
map<string,int>::iterator it = m_count.find("countA");
map<string,int>::iterator itc =m_count.find("total");
map<string,float>::iterator itp=m_percent.find("countA");
if(it== m_count.end())//use countA
{
m_count.insert(make_pair("countA",1));
}
else
{
int &c = it->second;
if(itc!=m_count.end()&&itp!=m_percent.end())
{
float f=(c+1)*100/(itc->second+1)*100.0
if (f<=itp->second)
{
it->second=it->second+1;
}
}
}
if(itc!=m_count.end())
{
itc->second=itc->second+1;
}
If you're talking about number of tasks with no regard to complexity, simply keep a count of how many jobs have been allocated to each. Let's call these counts countA for the jobs allocated to A, and count for the total number of jobs (for simplicity in calculations), and initialise them to zero.
Then, when a job comes in, allocate as per the following:
if count is equal to zero, allocate it to A and increment both countA and count.
otherwise, if countA / count is less than 0.6, allocate it to A and increment both countA and count.
otherwise allocate it to B and just increment count.
This will, over the long term, tend to average out allocations so that A gets 60%:
countA count countA/count allocateTo
------ ----- ------------ ----------
0 0 ? A
1 1 1.000 B
1 2 0.500 A
2 3 0.667 B
2 4 0.500 A
3 5 0.600 B
3 6 0.500 A
4 7 0.571 A
5 8 0.625 B
5 9 0.556 A
6 10 0.600 B
6 11 0.545 A
7 12 0.583 A
8 13 0.615 B
8 14 0.571 A
9 15 0.600 B
9 16 0.563 A
10 17 0.588 A
11 18 0.611 B
11 19 0.579 A
12 20 0.600
... and so on.
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)
}
}
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.
I have a tricky question about conditional sum in SAS. Actually, it is very complicated for me and therefore, I cannot explain it by words. Therefore I want to show an example:
A B
5 3
7 2
8 6
6 4
9 5
8 2
3 1
4 3
As you can see, I have a datasheet that has two columns. First of all, I calculated the conditional cumulative sum of column A ( I can do it by myself-So no need help for that step):
A B CA
5 3 5
7 2 12
8 6 18
6 4 8 ((12+8)-18)+6
9 5 17
8 2 18
3 1 10 (((17+8)-18)+3
4 3 14
So my condition value is 18. If the cumulative more than 18, then it equal 18 and next value if sum of the first value after 18 and exceeds amount over 18. ( As I said I can do it by myself )
So the tricky part is I have to calculate the cumulative sum of column B according to column A:
A B CA CB
5 3 5 3
7 2 12 5
8 6 18 9.5 (5+(6*((18-12)/8)))
6 4 8 5.5 ((5+6)-9.5)+4
9 5 17 10.5 (5.5+5)
8 2 18 10.75 (10.5+(2*((18-7)/8)))
3 1 10 2.75 ((10.5+2)-10.75)+1
4 3 14 5.75 (2.75+3)
As you can see from example the cumulative sum of column B is very specific. When column CA is equal to our condition value (18), then we calculate the proportion of the last value for getting our condition value (18) and then use this proportion for computing cumulative sum of column B.
Looks like when the sum of A reaches 18 or more you want to split the values of A and B between the current and the next record. One way is to remember the left over values for A and B and carry them forward in your new cumulative variables. Just make sure to output the observation before resetting those variables.
data want ;
set have ;
ca+a;
cb+b;
if ca >= 18 then do;
extra_a=ca - 18;
extra_b=b - b*((a - extra_a)/a) ;
ca=18;
cb=cb-extra_b ;
end;
output;
if ca=18 then do;
ca=extra_a;
cb=extra_b;
end;
drop extra_a extra_b ;
run;
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 6 years ago.
Improve this question
I have a for loop and a variable C.
The loop begins at 0 and is expected to run C times but actually it runs C-1 times.
Here is my code :
vector<int> v(1000) //Allocated globally
int M, S, C;
cin>>M>>S>>C;
//cout<<M<<" "<<S<<" "<<C;
int fi=0, la=0;
for(int i=0; i<C; i++)
{
int f;
cin>>f;
if(i==0l){ fi = f;}
v[f] = f;
cout<<i<<" "<<f<<" "<<v[f]<<endl;
if(i==C-1){ la = f;}
}
This is my test case -
3 27 16
2
3
5
6
8
9
10
13
14
15
16
19
20
21
22
27
Output by Xcode :
0 2 2
1 3 3
2 5 5
3 6 6
4 8 8
5 9 9
6 10 10
7 13 13
8 14 14
9 15 15
10 16 16
11 19 19
12 20 20
13 21 21
14 22 22
I use Xcode on Mac if it makes a difference.
The variables fi and la are to find the first and the last element of the list.
I want to know what is wrong in my code for the for loop and why is it not iterating C times.
Thanks
Your loop is iterating C times. This is the classic Zero-Based Numbering issue.
Let me explain using your example where C is 16 and a numbered list:
2
3
5
6
8
9
10
13
14
15
16
19
20
21
22
27
So you see your lopp did iterate 16 times. To go from 0 to 16 would have actually been iterating one more time, so 17 times.
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