题意:有一壶水, 体积在 LLL
和 RRR
之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1),
并且使得壶里剩下水体积不大于1. 你无法测量壶里剩下水的体积, 问最小需要倒水的次数。
析:考虑倒水的大致过程,不妨设 L > 0
。首先向一个杯子倒 L/2 2L
升水,再往另一个杯子倒 L/2+12L+1
升水。接下来就来回往两个杯子里倒 2 升,
直到倒空为止。这样就很容易分析出需要倒水的次数。唯一注意的是最后壶里面可以剩下 1 升水,可以省一次倒水的操作。
就是一步一步的模拟就可以,注意特殊情况,并且这个特殊情况较多。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const LL LNF = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 5; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } inline LL Min(LL a, LL b){ return a < b ? a : b; } inline LL Max(LL a, LL b){ return a > b ? a : b; } inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int main(){ LL l, r; while(scanf("%I64d %I64d", &l, &r) == 2){ if(!l){ if(r <= 1) printf("0\n"); else printf("%I64d\n", (r+1)/2); } else if(1 == l){ if(r == 1) printf("0\n"); else printf("%I64d\n", (1+r)/2); } else if(2 == l){ if(r == 2) printf("1\n"); else if(r >= 3 && r <= 5) printf("2\n"); else printf("%I64d\n", (r-l-2)/2+2); } else{ LL ans = 2; ans += (r-l-2)/2; ans = Max(ans, 2LL); printf("%I64d\n", ans); } } return 0; }