戳这里:HDU 4004
//思路:二分经典入门题...贪心判方案是否可行
1 #include "bits/stdc++.h" 2 using namespace std; 3 int L, n, m; 4 int pos[500010], dis[500010]; 5 6 bool Cant(int Dis_Jump) 7 { 8 int i, Dis_Sum = 0, Count = 0; 9 for(i = 1; i <= n + 1; ++i) { 10 if(dis[i] > Dis_Jump) { 11 return 1; 12 } 13 Dis_Sum += dis[i]; 14 if(Dis_Sum > Dis_Jump) { 15 ++Count; 16 Dis_Sum = dis[i]; 17 } 18 } 19 ++Count; 20 if(Count > m) 21 return 1; 22 return 0; 23 } 24 25 int main() 26 { 27 while(scanf("%d%d%d", &L, &n, &m) != EOF) { 28 int i; 29 for(i = 1; i <= n; ++i) { 30 scanf("%d", &pos[i]); 31 } 32 sort(pos + 1, pos + 1 + n); 33 for(i = 1; i <= n; ++i) { 34 dis[i] = pos[i] - pos[i - 1]; 35 } 36 dis[i] = L - pos[i - 1]; 37 // for(i = 1; i <= n + 1; ++i) { 38 // printf(" ee i == %d dis == %d\n", i, dis[i]); 39 // } 40 41 int l = 0, r = L, mid; 42 while(l <= r) { 43 mid = (l + r) >> 1; 44 if(Cant(mid)) { 45 l = mid + 1; 46 } 47 else { 48 r = mid - 1; 49 } 50 } 51 printf("%d\n", l); 52 } 53 }
HDU 4004 The Frog's Games 二分 贪心
时间: 2024-10-22 11:50:15