Really not sure if this is correct for x > 1 ... |
Thank you very much! It seems to me that it works for x>1 (I checked).
I tried to solve it and I found task like task below is.
Given a segment (stick) [0, N], which needs to be cut at the marked places L[1], L[2], ..., L[k]. The numbers N, L[i] are natural numbers, 1 <= N <= 1,000,000, 1 <= k <= 200, 0 <L[i] <N. The numbers L[i] are different. At a time, we can cut one segment into two and pay for this a price equal to its length. Make cuts in the marked places, paying the minimum price for this.
Input. The first line of the input file contains the length of the stick N and the number of cutting points k. The second line contains the coordinates of the cutting points L[1] L[2] ... L[k].
Output. Output the cutting pattern in the first line in any format. In the second line, output the minimum price for sawing.
What can you say about it? It sееms to me that It is the same task.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <stdio.h>
#include <sys/types.h>
#include <iostream>
#define K 202
#define INFTY INT_MAX
int k, n;
int a[K][K];
int l[K];
void print_brackets(int start, int end);
int main(int argc, char* argv[]) {
int i, j, m, k, n;
std::cin >> n >> k;
l[0] = 0;
for(i = 1 ; i <= k ; i++) std::cin >> l[i];
l[++k] = n;
for(i = 0 ; i <= k; i++) a[i][i] = a[i][i+1] = 0;
for(m = 2; m <= k ; m++)
for(i = 0 ; i + m <= k ; i++) {
int L = l[i+m] - l[i];
a[i][i+m] = INFTY;
for(j = 1 ; j < m; j++)
if(a[i][i+m] > a[i][i+j] + a[i+j][i+m])
a[i][i+m] = a[i][i+j] + a[i+j][i+m];
a[i][i + m] += L;
}
print_brackets (0, k);
putc('\n', stdout);
std::cout << a[0][k];
system("pause");
return 0;
}
void print_brackets(int start, int end) {
int L = l[end] - l[start];
int i, j, m;
if(end - start <= 1){
for(i = start ; i < end; i++)
std::cout << l[i];
std::cout << l[end];
}
else
for(j = 1 ; j < end - start ; j++ )
if(a[start][end] == a[start][start+j] + a[start+j][end] + L) {
std::cout << "( ";
print_brackets(start, start+j );
std::cout << ", ";
print_brackets(start+j, end ) ;
std::cout << " )";
break;
}
}
| |