博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[hdu3486]rmq+枚举优化
阅读量:5241 次
发布时间:2019-06-14

本文共 4357 字,大约阅读时间需要 14 分钟。

题意:给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 
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
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
pii; 53 typedef multiset
msi; 54 typedef set
si; 55 typedef vector
vi; 56 typedef map
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
T gcd(T a, T b){ return b==0?a:gcd(b,a%b);} 70 template
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
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
bool max_update(T &a,const T &b){ if(b>a){a = b; return true;}return false;} 73 template
bool min_update(T &a,const T &b){ if(b
T condition(bool f, T a, T b){ return f?a:b;} 75 template
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

 

转载于:https://www.cnblogs.com/jklongint/p/4427324.html

你可能感兴趣的文章
bzoj5483: [Usaco2018 Dec]Balance Beam
查看>>
C#委托Code
查看>>
ES6__变量的解构赋值
查看>>
Linux摄像头驱动2:UVC-摄像头驱动框架分析
查看>>
小电容,大作用(一)(转载于周立功网站)
查看>>
jquery学习记录
查看>>
Oracle中,将毫秒数转换为timestamp类型的两种方法
查看>>
用CGI作为unity3d服务器存储数据
查看>>
关于Django的理解
查看>>
Eclipse最有用快捷键整理
查看>>
读《程序是怎么跑起来的》第9章
查看>>
jmeter+ant+jenkins 搭建接口自动化测试
查看>>
Video.js
查看>>
IFM设备 Linux方面资料
查看>>
ubunt 14.04 Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul
查看>>
windows操作系统查看端口,关闭端口进程
查看>>
WINCE6.0 CAB文件的制作与安装
查看>>
python:UnboundLocalError: local variable 'xxx' referenced before assignment
查看>>
项目冲刺集合贴
查看>>
poi导入excel
查看>>