C |
Optimal Segments Input: Standard Input Output: Standard Output |
Consider a grid of size 1 x N. Each cell of the grid has the following properties
· Cell C of the grid has a value of VC(1 ≤ C ≤ N)
· The value of each cell is a positive integer less than26
· Some of the cells arespecial and they are represented with the character X
· Cell C has a weight of (two to the power of cell value)
· The special cells have weights of1
You will be given the values of these N cells and your job will be to divide these intoK segments so that
· Each segment contains at least one cell
· There is at least onespecial cell in each segment
The weight of a segment is equal to the product of the weights of the cells it contains. You have to form segments in such a way so that ratio
(Highest weight of all the segments) / (Lowest weight of all the segments) is minimized.
In case there are multiple answers with the same lowest ratio, you have to make sure the number of cells in the first segment is maximized. If there is still a tie, then make sure the number of cells in the second segment is maximized and so on.
Example:
N = 5 and K = 2
Cell values = {1 2 X 3 X }
Cell weights = {2 4 1 8 1}
Optimal segmentation = (2 4 1)(8 1)
Weights of segments = (8)(8)
Ratio = 1
Final Result = (1 2 X)(3 X)
Input
The first line of input is an integer T(T ≤ 200) that indicates the number of test cases. Each case starts with two integersN(1 < N < 31) and K(1 < K < 16). The meaning ofN and K are mentioned above. The next line containsN integers where the Ith integer gives the value ofVI. The integers that are special will be represented byX.
Output
For each case, output the case number first. If there is no way to divide the N cells into K segments, meeting the constraints above, then print “not possible!” If there is a way but the ratio is greater than1015 then print “overflow”. If the ratio is not greater than 1015 then output the ratio first followed by the segmentations. Each segment is enclosed by brackets. Look at the output for detailed format.
Sample Input Output for Sample Input
4 5 2 1 2 X 3 X 6 3 X X 2 3 4 5 10 3 X X X 25 25 25 25 25 25 25 10 3 4 X 3 1 X 3 X X 3 2 |
Case 1: 1 (1 2 X)(3 X) Case 2: not possible! Case 3: overflow Case 4: 8 (4 X 3)(1 X 3 X)(X 3 2) |
这题我用了一个特别笨的方法,先用状态DP算好每个状态的方法数,在取个差值最小的!
int dp[n][k]表示遍历到第n个数时要划分成k分的可行性,可以为1,不可以为0;
vector<ll> dp_max[n][k]表示遍历到第n个数时要划分成k分,所有情况的最大值;
vector<ll> dp_min[n][k]表示遍历到第n个数时要划分成k分,所有情况的最小值;
vector<ll> Next[n][k]表示遍历到第n个数时要划分成k分,所以情况的下一划;
vector<ll> Next[n][k]表示遍历到第n个数时要划分成k分,所以情况来自下一划的第几个情况。
最后:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <vector>
using namespace std;
#define ll long long
const int maxn = 35;
int vis[maxn][maxn];
ll dp[maxn][maxn] ,value[maxn];
vector<ll> dp_max[maxn][maxn] , dp_min[maxn][maxn] , next[maxn][maxn] , Next[maxn][maxn];
string num[maxn];
int N , K;
void initial(){
for(int i = 0 ; i < maxn ; i++){
for(int j = 0 ; j < maxn ; j++){
dp[i][j] = 0;
next[i][j].clear();
Next[i][j].clear();
dp_max[i][j].clear();
dp_min[i][j].clear();
vis[i][j] = 0;
}
value[i] = 0;
num[i].clear();
}
}
void readcase(){
scanf("%d%d" , &N , &K);
for(int i = 0 ; i < N ; i++){
cin >> num[i];
if(num[i] != "X"){
stringstream ss;
ss << num[i];
ss >> value[i];
}
}
}
ll DP(int n , int k){
if(N-n < k) return 0;
if(n >= N){
if(k == 0) return 1;
return 0;
}
if(vis[n][k] == 1){
return dp[n][k];
}
vis[n][k] = 1;
ll sum = 0 , T = 0;
for(int i = n;i < N;i++){
sum += value[i];
if(num[i] == "X") T = 1;
if(T && DP(i+1 , k-1)){
dp[n][k] = 1;
for(int j = 0;j < dp_max[i+1][k-1].size();j++){
dp_max[n][k].push_back(max(sum , dp_max[i+1][k-1][j]));
dp_min[n][k].push_back(min(sum , dp_min[i+1][k-1][j]));
Next[n][k].push_back(i+1);
next[n][k].push_back(j);
}
}
}
return dp[n][k];
}
void out(int len){
int n = 0 , tNext = Next[0][K][len], tnext = next[0][K][len], k = K;
while(tNext != -1){
int i = 0;
while(n < tNext){
if(i == 0){
printf("(%s" , num[n].c_str());
}else{
printf(" %s" , num[n].c_str());
}
n++;
i++;
}
printf(")");
int t = tnext;
tnext = next[n][k-1][t];
tNext = Next[n][k-1][t];
k--;
}
printf("\n");
}
void computing(){
dp_min[N][0].push_back(1e16);
dp_max[N][0].push_back(0);
Next[N][0].push_back(-1);
next[N][0].push_back(0);
if(DP(0 , K)){
ll ans = 1e16;
int k = 0;
for(int i = 0 ; i < Next[0][K].size() ; i++){
if(ans >= dp_max[0][K][i]-dp_min[0][K][i]){
ans = dp_max[0][K][i]-dp_min[0][K][i];
k = i;
}
}
if(ans >= 50){
printf("overflow\n");
}else{
printf("%lld " , ((long long)1 << ans));
out(k);
}
}else{
printf("not possible!\n");
}
}
int main(){
int t;
scanf("%d" , &t);
for(int i = 1;i <= t;i++){
initial();
readcase();
printf("Case %d: " , i);
computing();
}
return 0;
}