• [hdu3486]rmq+枚举优化


    题意:给n个数,求最小的段数,使得每一段的最大值之和大于给定的k。每一段的长度相等,最后若干个丢掉。

    思路:从小到大枚举段数,如果能o(1)时间求出每一段的和,那么总复杂度是O(n(1+1/2+1/3+...+1/n))=O(nlogn)的。但题目时限卡得比较紧,需加一点小优化,如果连续两个段数它们每一段的个数一样,那么这次只比上次需要多计算一个区间,用上一次的加上这个区间最大值得到当前分段的总和,这样能减少不少运算量。详见代码:

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2 
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <set>
     16 #include <bitset>
     17 #include <functional>
     18 #include <numeric>
     19 #include <stdexcept>
     20 #include <utility>
     21 
     22 using namespace std;
     23 
     24 #define mem0(a) memset(a, 0, sizeof(a))
     25 #define lson l, m, rt << 1
     26 #define rson m + 1, r, rt << 1 | 1
     27 #define define_m int m = (l + r) >> 1
     28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     31 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     32 #define all(a) (a).begin(), (a).end()
     33 #define lowbit(x) ((x) & (-(x)))
     34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     37 #define pchr(a) putchar(a)
     38 #define pstr(a) printf("%s", a)
     39 #define sstr(a) scanf("%s", a);
     40 #define sint(a) ReadInt(a)
     41 #define sint2(a, b) ReadInt(a);ReadInt(b)
     42 #define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c)
     43 #define pint(a) WriteInt(a)
     44 #define if_else(a, b, c) if (a) { b; } else { c; }
     45 #define if_than(a, b) if (a) { b; }
     46 #define test_print1(a) cout << "var1 = " << a << endl
     47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = b" << ", var3 = " << c << endl
     49 
     50 typedef double db;
     51 typedef long long LL;
     52 typedef pair<int, int> pii;
     53 typedef multiset<int> msi;
     54 typedef set<int> si;
     55 typedef vector<int> vi;
     56 typedef map<int, int> mii;
     57 
     58 const int dx[8] = {0, 0, -1, 1};
     59 const int dy[8] = {-1, 1, 0, 0};
     60 const int maxn = 4e5 + 7;
     61 const int maxm = 1e3 + 7;
     62 const int maxv = 1e7 + 7;
     63 const int max_val = 1e6 + 7;
     64 const int MD = 22;
     65 const int INF = 1e9 + 7;
     66 const double pi = acos(-1.0);
     67 const double eps = 1e-10;
     68 
     69 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     70 template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-'0';c=getchar();}}
     71 template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr('0'+b[j]);}
     72 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     73 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     74 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     75 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     76 int make_id(int x, int y, int n) { return x * n + y; }
     77 
     78 int f[maxn][20], t[maxn], a[maxn], sum[maxn];
     79 int n;
     80 void RMQ_Init() {
     81     rep_up0(i, n) f[i][0] = a[i];
     82     rep_up1(j, 18) {
     83         for (int i = 0; i + (1 << j) - 1 < n; i++) {
     84             f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
     85         }
     86     }
     87 }
     88 int RMQ(int L, int R) {
     89     int p = t[R - L + 1];
     90     return max(f[L][p], f[R - (1 << p) + 1][p]);
     91 }
     92 LL getSum(int t, int x) {
     93     LL sum = 0;
     94     rep_up0(i, t) {
     95         sum += RMQ(i * x, i * x + x - 1);
     96     }
     97     return sum;
     98 }
     99 int main() {
    100     //freopen("in.txt", "r", stdin);
    101     //freopen("out.txt", "w", stdout);
    102     int k;
    103     rep_up1(i, 18) {
    104         for (int j = (1 << (i - 1)) + 1; j <= (1 << i); j++) t[j] = i - 1;
    105     }
    106     while (cin >> n >> k, n >= 0 || k >= 0) {
    107         rep_up0(i, n) {
    108             sint(a[i]);
    109             if (i) sum[i] = sum[i - 1] + a[i];
    110             else sum[i] = a[i];
    111         }
    112         int ans = -1, last_sum = 0;
    113         RMQ_Init();
    114         for (int i = 1; i <= n; i++) {
    115             int x = n / i;
    116             LL sum = 0;
    117             if (i > 1 && n / (i - 1) == x) sum = last_sum + RMQ(x * (i - 1), x * i - 1);
    118             else sum = getSum(i, x);
    119             if (sum > k) {
    120                 ans = i;
    121                 break;
    122             }
    123             last_sum = sum;
    124         }
    125         cout << ans << endl;
    126     }
    127     return 0;
    128 }
    View Code
  • 相关阅读:
    与众不同 windows phone (50)
    与众不同 windows phone (49)
    重新想象 Windows 8.1 Store Apps (93)
    重新想象 Windows 8.1 Store Apps 系列文章索引
    重新想象 Windows 8.1 Store Apps (92)
    重新想象 Windows 8.1 Store Apps (91)
    重新想象 Windows 8.1 Store Apps (90)
    重新想象 Windows 8.1 Store Apps (89)
    重新想象 Windows 8.1 Store Apps (88)
    重新想象 Windows 8.1 Store Apps (87)
  • 原文地址:https://www.cnblogs.com/jklongint/p/4427324.html
Copyright © 2020-2023  润新知