#include #include #include #include #include using namespace std; int getmin(int a, int b) { if (a < b) { return a; } return b; } int main() { int testCases; scanf ("%d", &testCases); for (int tc = 0; tc < testCases; tc++) { int height; int previousLineCost = 1147483648; int previousLineLeft = -1; int fromRightCost = 1147483648; int previousLineRight = -1; int bestCost = 1147483648; scanf("%d", &height); for (int i = 0; i < height; i++) { int left, right; scanf("%d %d", &left, &right); int horizontally = right - left; int verticallyByLeft = 1147483648; int verticallyByRight = 1147483648; if (previousLineLeft != -1) { verticallyByLeft = (previousLineLeft - left) + 1 + previousLineCost; } if (previousLineRight != -1) { verticallyByRight = (right - previousLineRight) + 1 + fromRightCost; } previousLineCost = getmin(horizontally, verticallyByLeft); fromRightCost = getmin(horizontally, verticallyByRight); previousLineLeft = left; previousLineRight = right; if (previousLineCost < bestCost) { bestCost = previousLineCost; } if (fromRightCost < bestCost) { bestCost = fromRightCost; } } printf("K prechodu reky je treba %d pontonu.\n", bestCost); } }