1、晚上一点多爬起来太痛苦了,脑子晕乎乎的,第一个都没做出来~~~~
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <stack> 8 #include <algorithm> 9 #include <list> 10 #include <ctime> 11 #include <set> 12 #include <queue> 13 typedef long long ll; 14 using namespace std; 15 16 class TheArray { 17 public: 18 int find(int n, int d, int first, int last) { 19 if (first > last) 20 swap(first, last); 21 if(0==d) 22 return first; 23 int res; 24 int cost=(last-first)/d; 25 int start=first+cost*d; 26 int r=(n-1-cost); 27 if(last==start){ 28 if((r%2)==0){ 29 res=start+d*(r/2); 30 }else 31 res=start+d*((r-1)/2); 32 }else{ 33 if((r%2)==0){ 34 res=start+d*(r/2); 35 }else 36 res=last+d*((r-1)/2); 37 } 38 return res; 39 } 40 };
2、二分法的应用
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <stack> 8 #include <algorithm> 9 #include <list> 10 #include <ctime> 11 #include <set> 12 #include <queue> 13 typedef long long ll; 14 using namespace std; 15 16 class TheOlympiadInInformatics { 17 public: 18 int find(vector<int> sums, int k) { 19 int sz = sums.size(); 20 ll start = 1; 21 ll end = 1000000001; 22 int res = 0; 23 if (k == 0) { 24 for (int i = 0; i < sz; i++) { 25 res = max(res, sums[i]); 26 } 27 return res; 28 } 29 30 while (start < end) { 31 ll mid = (start + end) / 2; 32 ll num = 0; 33 for (int i = 0; i < sz; i++) { 34 num += (sums[i] / mid); 35 } 36 if (num > k) { //不行 37 start = mid + 1; 38 } else { //行 39 end = mid; 40 } 41 } 42 res = end-1; 43 return res; 44 } 45 };