• 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 };
  • 相关阅读:
    flume1.7.0的安装与使用
    获取top10
    editplus格式化xml文档
    LOG4J.PROPERTIES配置详解
    Oracle自增列
    javascript 传递引用类型参数
    {JavaScript}栈和堆内存,作用域
    JAVA中String与StringBuffer的区别
    Java中堆和栈的区别(转)
    JAVA错误:org.apache.jasper.JasperException: java.lang.ClassCastException:org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apach
  • 原文地址:https://www.cnblogs.com/kakamilan/p/2952655.html
Copyright © 2020-2023  润新知