通过一系列加压减压的步骤来对轮胎进行检测
胎压小于0或者大于K(0<K<=200)就会损坏轮胎
给出N(0<N<=8)个检测步骤,每步先加压后减压,顺序可以调整
求使得轮胎正常通过测试的最小初始气压,如果无法通过测试输出-1
输入:
用例数T
N, K
N个加压
N个减压
输出:使得轮胎正常通过测试的最小初始气压
5
3 100
75 45 80
30 50 95
2 100
65 90
20 30
5 150
35 105 100 45 75
115 75 55 35 105
7 150
20 95 15 65 85 75 55
105 80 10 90 115 110 45
8 200
35 30 50 80 70 15 10 40
70 20 20 85 65 40 25 50
#1 15
#2 -1
#3 25
#4 -1
#5 45
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int T, N, K, in[8], out[8], answer;
bool flag[8];
void dfs(int currentPressure, int max, int min, int finished){
if (answer <= 0 - min){
return;
}
if (max - min > K){
return;
}
if (finished == N){
answer = 0 - min;
}
for (int i = 0; i < N; i++){
if (!flag[i]){
flag[i] = true;
int first = currentPressure + in[i];
int second = currentPressure + in[i] - out[i];
dfs(second, first > max ? first : max, second < min ? second : min, finished + 1);
flag[i] = false;
}
}
}
int main(int argc, char** argv) {
freopen("sample_input.txt", "r", stdin);
setbuf(stdout, NULL);
scanf("%d", &T);
for (int test_case = 1; test_case <= T; ++test_case){
scanf("%d%d", &N, &K);
for (int i = 0; i < N; i++){
scanf("%d", &in[i]);
}
for (int i = 0; i < N; i++){
scanf("%d", &out[i]);
flag[i] = false;
}
answer = K;
dfs(0, 0, 0, 0);
if (answer == K){
answer = -1;
}
printf("#%d %d\n", test_case, answer);
}
return 0;
}