cost of cutting a plank - c++

I know that this question has been asked but i cant understand the problem in my code. I know that we have to use cost in desending order for minimum cost and i did same but still gives wrong output.
A board composed of m×n wooden squares and asks him to find the minimum cost of breaking the board back down into individual 1×1 pieces. To break the board down, Bob must make cuts along its horizontal and vertical lines.
To reduce the board to squares, xn−1 vertical cuts must be made at locations x1,x2,…,xn−2,xn−1 and ym−1 horizontal cuts must be made at locations y1,y2,…,ym−2,ym−1. Each cut along some xi (or yj) has a cost, cxi (or cyj). If a cut of cost c passes through n already-cut segments, the total cost of the cut is n×c.
The cost of cutting the whole board down into 1×1 squares is the sum of the cost of each successive cut. Recall that the cost of a cut is multiplied by the number of already-cut segments it crosses through, so each cut is increasingly expensive.
Input Format
The first line contains a single integer, T, denoting the number of test cases. The subsequent 3T lines describe each test case in 3 lines.
For each test case, the first line has two positive space-separated integers, m and n, detailing the respective height (y) and width (x) of the board.
The second line has m−1 space-separated integers listing the cost, cyj, of cutting a segment of the board at each respective location from y1,y2,…,ym−2,ym−1.
The third line has n−1 space-separated integers listing the cost, cxi, of cutting a segment of the board at each respective location from x1,x2,…,xn−2,xn−1.
Note: If we were to superimpose the m×n board on a 2D graph, x0, xn, y0, and yn would all be edges of the board and thus not valid cut lines.
Constraints
1≤T≤20
2≤m,n≤1000000
,0≤cxi,cyj≤1000000000
Output Format
For each of the T test cases, find the minimum cost (MinimumCost) of cutting the board into 1×1 squares and print the value of MinimumCost % (1000000000+7).
#include <iostream>
#include <limits>
using namespace std;
int main() {
int t,ch=0;
long int pos,m,n,h=1,l=1;
long long int cost=0,*x,*y,temp;
cin>>t;
while(t>0)
{cin>>m>>n;
cost=0;
x = new long long int[n-1];
y = new long long int[m-1];
for (long i=0;i<m-1;i++)
cin>>y[i];
for(long i=0;i<n-1;i++)
cin>>x[i];
h=1;
l=1;
while((h!=m)|(l!=n))
{ch=0;
temp=0;
for (long i=0;i<m-1;i++)
if (temp<y[i])
{temp=y[i];
pos=i;
}
for(long i=0;i<n-1;i++)
if (temp<x[i])
{temp=x[i];
pos=i;
ch=1;
}
cost=cost+temp*(ch==0?l:h);
if (ch==0)
{y[pos]=-1;
h++;}
else
{x[pos]=-1;
l++;
}
}
cout<<cost%1000000007;
t--;
}
return 0;
}
Test case that gives wrong output:
Input:
5
52 30
2 30 79 47 4 56 47 67 25 30 75 58 47 54 66 61 6 64 28 41 75 36 1 92 42 61 35 56 12 86 84 14 68 63 13 72 19 60 39 96 43 14 55 42 21 73 3 27 37 84 68
64 72 21 56 14 35 44 71 47 82 7 14 50 71 79 23 42 92 14 39 35 81 46 29 2 19 84 81 57
23 43
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
60 76
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
58 40
71 58 61 51 33 3 43 48 94 30 29 40 59 83 12 43 64 69 64 65 42 57 40 72 64 98 98 47 56 6 85 79 65 46 30 98 49 25 98 96 7 27 88 66 10 0 62 26 69 78 92 64 87 84 88 51 35
87 50 91 45 35 22 62 81 53 61 83 30 59 31 38 39 19 56 1 20 70 28 41 48 72 57 35 56 46 39 91 85 41 34 30 77 57 93 10
47 94
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17
Output
51028
1912
33638
91124
27525

This line could be a contributing factor:
while((h!=m)|(l!=n))
The | operator is a binary arithmetic operator.
Maybe you were looking for ||, which is the logical OR operator:
while((h!=m) || (l!=n))

Related

VLOOKUL OR OFFSET

I have two sheets in an Excel workbook. I need the formula which creates tables by using vlookup.
I have 10 columns and 10 rows like this
1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
I have tried to use Vlookup with sum but not get the actual results.
The expected result should be like this
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 42 48 56 64 72 80
9 18 27 36 49 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Need help to understand QRegularExpression for my tasks

I have a QTextStream which contain:
Line1: 3 5 7 17 19 23 25
Line2: 3 5 7 17 19 23 26
Line3: 3 5 8 17 19 23 27
Line4: 3 5 9 17 21 35 37
Line5: 3 5 10 17 21 35 38
Line6: 3 5 11 17 21 35 39
Line7: 3 5 12 17 21 36 37
Line8: 3 5 13 17 21 37 38
Line9: 3 5 15 17 21 36 39
Line10: 3 5 16 17 21 37 38
I need to create regular expression to select only lines which contain numbers 3, 17 and 37.
Line4, 7, 8 and 10.
How to setup pattern?
Assuming they are ordered, you might use \b3\b.*\b17\b.*\b37\b.
So in C++, with raw string R"(\b3\b.*\b17\b.*\b37\b)".

Having trouble importing data with sets in SAS

data testData;
INFILE DATALINES;
INPUT crop $ 1-11 [month1] 12-23 [month2] 25-36 [month3] 38-49;
datalines;
Corn 16 27 31 33 15 23 30 30 16 27 27 26
Corn 18 20 25 23 15 15 31 32 15 32 32 15
Corn 12 15 16 73 . . . . . . . .
Soybeans 20 23 23 25 24 24 25 32 21 25 23 24
Soybeans 27 45 24 12 12 13 15 42 22 32 31 43
Cotton 31 32 33 34 29 24 26 28 34 32 28 45
Cotton 26 25 23 24 53 48 75 26 34 35 25 78
Sugarbeets 22 23 25 42 25 25 24 26 34 25 16 52
Sugarbeets 54 23 21 54 25 43 32 15 26 54 2 54
Clover 12 45 32 54 24 58 25 34 87 54 61 21
Clover 51 31 31 16 96 48 54 62 31 31 11 11
Clover 56 13 13 71 32 13 27 32 36 26 54 32
Clover 53 08 06 54 32 32 62 16 . . . .
;
I don't have much guidance on what this data actually is (it's a school question) so I'm assuming that the number sets are harvests per week per month. I'm fairly new to SAS so I might be going in the complete wrong direction, but I thought tossing the months in arrays would be the best bet. I added the periods because there is some missing data.
The issue with this code is that I get this error:
ERROR 22-322: Expecting a name.
ERROR: Undeclared array referenced: NAME.
But it seems like the [month1] should be naming the array, right? I've also tried this:
data testData;
INFILE DATALINES;
array crop{*} $ 1-11;
array month1{*} 12-23;
array month2{*} 25-36;
array month3{*} 38-49;
datalines;
Corn 16 27 31 33 15 23 30 30 16 27 27 26
Corn 18 20 25 23 15 15 31 32 15 32 32 15
Corn 12 15 16 73 . . . . . . . .
Soybeans 20 23 23 25 24 24 25 32 21 25 23 24
Soybeans 27 45 24 12 12 13 15 42 22 32 31 43
Cotton 31 32 33 34 29 24 26 28 34 32 28 45
Cotton 26 25 23 24 53 48 75 26 34 35 25 78
Sugarbeets 22 23 25 42 25 25 24 26 34 25 16 52
Sugarbeets 54 23 21 54 25 43 32 15 26 54 2 54
Clover 12 45 32 54 24 58 25 34 87 54 61 21
Clover 51 31 31 16 96 48 54 62 31 31 11 11
Clover 56 13 13 71 32 13 27 32 36 26 54 32
Clover 53 08 06 54 32 32 62 16 . . . .
;
I'm not sure which method I should be using or if I'm even going in the right direction with this dataset.
For what I can tell you have crop value for 12 separate time points - so twelve months?
In the input statement you just need to define a variable name for all your columns which you are providing in your "datalines" statement. This can just be defined as:
data testData;
INFILE DATALINES;
Input Crop $ month1-month12;
datalines;
Corn 16 27 31 33 15 23 30 30 16 27 27 26
Corn 18 20 25 23 15 15 31 32 15 32 32 15
Corn 12 15 16 73 . . . . . . . .
Soybeans 20 23 23 25 24 24 25 32 21 25 23 24
Soybeans 27 45 24 12 12 13 15 42 22 32 31 43
Cotton 31 32 33 34 29 24 26 28 34 32 28 45
Cotton 26 25 23 24 53 48 75 26 34 35 25 78
Sugarbeets 22 23 25 42 25 25 24 26 34 25 16 52
Sugarbeets 54 23 21 54 25 43 32 15 26 54 2 54
Clover 12 45 32 54 24 58 25 34 87 54 61 21
Clover 51 31 31 16 96 48 54 62 31 31 11 11
Clover 56 13 13 71 32 13 27 32 36 26 54 32
Clover 53 08 06 54 32 32 62 16 . . . .;
run;
Arrays are used to refer to already existing variables, so to my knowledge not to declare variables as you want to do here.

Access values from Matrix in OpenCV

For example, I have a matrix M of size 10x10 and I have a column matrix ind of length 5
How can I assign A(ind,:) to a new matrix B in C++ with OpenCV?
Below is how I do in Matlab:
A = [ 41 8 33 36 22 14 38 43 18 4
46 49 2 2 20 34 13 13 42 3
7 48 43 14 39 33 26 41 30 27
46 25 47 3 40 9 35 13 28 39
32 41 34 5 10 6 45 47 46 47
5 8 38 42 25 25 48 18 15 7
14 22 38 35 23 48 28 10 38 29
28 46 20 16 33 18 7 13 38 24
48 40 33 48 36 30 8 31 20 1
49 48 9 2 38 12 13 24 29 17]
ind = [2; 8; 4; 6; 2]
B = A(ind, :);
B = [ 46 49 2 2 20 34 13 13 42 3
28 46 20 16 33 18 7 13 38 24
46 25 47 3 40 9 35 13 28 39
5 8 38 42 25 25 48 18 15 7
46 49 2 2 20 34 13 13 42 3]
Can anyone tell me how to do this in C++ with OpenCV without using for loop
There is no direct way to extract a random ordering of rows/columns without iterating in some way. The simplest method is to extract rows and push them into the target matrix one by one. Given you have your matrix A declared and its data set:
cv::Mat B;
B.push_back(A(cv::Range(2,3),cv::Range::all()));
B.push_back(A(cv::Range(8,9),cv::Range::all()));
B.push_back(A(cv::Range(4,5),cv::Range::all()));
B.push_back(A(cv::Range(6,7),cv::Range::all()));
B.push_back(A(cv::Range(2,3),cv::Range::all()));
should do what you want. This uses the overloaded operator()(cv::rowRange, cv::colRange) to extract the selected rows.
I don't think this is possible without using for loop but the fastest way of doing this is by using memcpy. You can see the complete code here

functioning of bitwise and

This question was asked in an interview, can someone tell what does the following code do? It gives output 15 for 150, 3 for 160, 15 for 15. What mathematical operation is it performing on 'n'.
int foo(int n)
{
int t,count=0;
t=n;
while(n)
{
count=count+1;
n=(n-1)&t;
}
return count;
}
It seems to calculate the number max(n**2-1, 0), where n is the number of 1 bits in a number's binary representation:
0 0 0b0
1 1 0b1
2 1 0b10
3 3 0b11
4 1 0b100
5 3 0b101
6 3 0b110
7 7 0b111
8 1 0b1000
9 3 0b1001
10 3 0b1010
11 7 0b1011
12 3 0b1100
13 7 0b1101
14 7 0b1110
15 15 0b1111
16 1 0b10000
17 3 0b10001
18 3 0b10010
19 7 0b10011
20 3 0b10100
21 7 0b10101
22 7 0b10110
23 15 0b10111
24 3 0b11000
25 7 0b11001
26 7 0b11010
27 15 0b11011
28 7 0b11100
29 15 0b11101
30 15 0b11110
31 31 0b11111
32 1 0b100000
33 3 0b100001
34 3 0b100010
35 7 0b100011
36 3 0b100100
37 7 0b100101
38 7 0b100110
39 15 0b100111
40 3 0b101000
41 7 0b101001
42 7 0b101010
43 15 0b101011
44 7 0b101100
45 15 0b101101
46 15 0b101110
47 31 0b101111
48 3 0b110000
49 7 0b110001
50 7 0b110010
51 15 0b110011
52 7 0b110100
53 15 0b110101
54 15 0b110110
55 31 0b110111
56 7 0b111000
57 15 0b111001
58 15 0b111010
59 31 0b111011
60 15 0b111100
61 31 0b111101
62 31 0b111110
63 63 0b111111
64 1 0b1000000
65 3 0b1000001
66 3 0b1000010
67 7 0b1000011
68 3 0b1000100
69 7 0b1000101
70 7 0b1000110
71 15 0b1000111
72 3 0b1001000
73 7 0b1001001
74 7 0b1001010
75 15 0b1001011
76 7 0b1001100
77 15 0b1001101
78 15 0b1001110
79 31 0b1001111
80 3 0b1010000
81 7 0b1010001
82 7 0b1010010
83 15 0b1010011
84 7 0b1010100
85 15 0b1010101
86 15 0b1010110
87 31 0b1010111
88 7 0b1011000
89 15 0b1011001
90 15 0b1011010
91 31 0b1011011
92 15 0b1011100
93 31 0b1011101
94 31 0b1011110
95 63 0b1011111
96 3 0b1100000
97 7 0b1100001
98 7 0b1100010
99 15 0b1100011
It is easier to find out the "mathematical operation", when function is changed to recursive:
int foo(int n, int t)
{
if( n )
return foo( (n-1) & t ) + 1
else
return 0;
}
So formula is:
F(0,t) = 0
F(n,t) = F( (n-1) & t, t ) + 1
foo(n) = F(n,n)
I don't have any idea, is that wellknown formula for counting something, or not.
You may find answer from math.stackexchange.com
That is a method known as Brian Kernighan's way to count set bits :
unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
v &= v - 1; // clear the least significant bit set
}
Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.
Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"