TCO 2013 1C

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 };

 

 

 

转载于:https://www.cnblogs.com/kakamilan/archive/2013/03/10/2952655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值