清北考前刷题day6下午好
- /*
- 贪心 负数一定不取
- 枚举最高位是1 且答案取为0的 位置, 更新答案。
- */
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #define ll long long
- #define N 100010
- using namespace std;
- int n;
- ll a[N],ans,sum[N];
- char s[N];
- ll read()
- {
- ll x=,f=;char c=getchar();
- while(c>''||c<''){if(c=='-')f=-;c=getchar();}
- while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
- return x*f;
- }
- int main()
- {
- freopen("maximum.in","r",stdin);
- freopen("maximum.out","w",stdout);
- scanf("%d",&n);
- for(int i=; i<=n; i++)
- {
- a[i]=read();
- sum[i]=sum[i-];
- if(a[i]>)sum[i]=sum[i-]+a[i];
- }
- scanf("%s",s+);
- ll c=;
- for(int i=n; i>=; i--)
- if(s[i]=='')
- {
- ans=max(ans,c+sum[i-]);
- c+=max(a[i],0LL);
- }
- ans=max(ans,c);
- printf("%I64d\n",ans);
- return ;
- }
- /*
- 15暴力
- */
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define N 1010
- using namespace std;
- int n,k;
- int l,r,mid;
- int num[N],tmp[N];
- int ans;
- int minn(int a,int b,int i)
- {
- if(a<b) return tmp[i-];
- else return tmp[i-]+mid;
- }
- bool check()
- {
- int sum=;
- for(int i=; i<=n; i++) tmp[i]=num[i];
- for(int i=; i<=n; i++)
- if(abs(tmp[i]-tmp[i-])>mid)
- {
- if(tmp[i+]>tmp[i]) tmp[i]=max(tmp[i],tmp[i-]+mid);
- else tmp[i]=minn(abs(tmp[i-]-tmp[i+]),tmp[i-]+mid-tmp[i+],i);
- sum++;
- }
- if(sum>k) return false;
- else return true;
- }
- int main()
- {
- scanf("%d%d",&n,&k);
- for(int i=; i<=n; i++) scanf("%d",&num[i]);
- l=;
- r=;
- while(l<=r)
- {
- mid=(l+r)/;
- if(check()) ans=mid,r=mid-;
- else l=mid+;
- }
- printf("%d",ans);
- }
不会分块
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int maxn = ;
- const int len = ;
- int n, A, B,m;
- int a[maxn],b[maxn],l[maxn],r[maxn],lb[maxn],tot;
- int answer[][];
- int rt[][maxn], lt[][maxn],d[maxn];
- //int root[maxn*len], ll[maxn*len], rr[maxn*len],data[maxn*len];
- int find(int x)
- {
- for (int l = , r = m; l < r;)
- {
- int mid = (l + r) / ;
- if (b[mid] == x) return mid;
- if (b[mid]>x) r = mid;
- else l = mid + ;
- }
- return -;
- }
- /*int build(int l, int r)
- {
- if (l == r)
- {
- tot++;
- ll[tot] = rr[tot] = 0;
- data[tot] = d[l];
- return tot;
- }
- tot++;
- int tmp = tot;
- ll[tmp] = build(l, (l + r) / 2);
- rr[tmp] = build((l + r) / 2 + 1, r);
- data[tmp] = min(data[ll[tmp]], data[rr[tmp]]);
- return tmp;
- }
- int change(int i, int l, int r,int x)
- {
- if (l == x && r == x)
- {
- tot++;
- ll[tot] = rr[tot] = 0;
- data[tot] = n + 1;
- return tot;
- }
- tot++;
- int tmp = tot;
- ll[tot] = ll[i];
- rr[tot] = rr[i];
- if (l <= x && x <= (l + r) / 2) ll[tmp] = change(ll[i], l, (l + r) / 2, x);
- else rr[tmp] = change(rr[i], (l + r) / 2 + 1, r, x);
- data[tmp] = min(data[ll[tmp]], data[rr[tmp]]);
- return tmp;
- }
- int check(int i, int l, int r, int x, int &ans)
- {
- if (l > x) return 0;
- if (1 <= l && r <= x)
- {
- ans = min(ans, data[i]); return 0;
- }
- check(ll[i], l, (l + r) / 2, x, ans);
- check(rr[i], (l + r) / 2 + 1, r, x, ans);
- return 0;
- }*/
- int main()
- {
- double ti = clock();
- freopen("sequence.in", "r", stdin);
- freopen("sequence.out", "w", stdout);
- scanf("%d%d%d", &n, &A, &B);
- a[] = ;
- for (int i = ; i <= n; i++)
- {
- char c;
- for (scanf(" %c", &c); c != 'A' && c != 'B'; scanf(" %c", &c));
- if (c == 'A') a[i] = a[i - ] + B;
- else a[i] = a[i - ] - A;
- }
- for (int i = ; i <= n; i++) b[i] = a[i];
- sort(b, b + n + );
- m = unique(b, b + n + ) - b;
- for (int i = ; i <= n; i++)
- {
- a[i] = find(a[i]);
- if (a[i] == -)
- {
- printf("???\n");
- return ;
- }
- }
- n++;
- int k = n / len;
- for (int i = ; i <= k; i++)
- {
- int ans = ;
- tot++;
- for (int j = i*len; j < n; j++)
- {
- if (j%len == )
- {
- answer[i][j / len] = ans;
- }
- if (lb[a[j]] != tot)
- {
- lb[a[j]] = tot;
- l[a[j]] = j;
- }
- ans = max(ans, j - l[a[j]]);
- }
- }
- for (int i = ; i < n; i++) r[i] = -;
- for (int i = ; i < n; i++)
- {
- r[a[i]] = i;
- if (i%len == len - )
- {
- int tmp = i / len;
- for (int j = ; j < n; j++)
- rt[tmp][j] = r[j];
- }
- }
- for (int i = ; i < n; i++) l[i] = -;
- for (int i = n - ; i >= ; i--)
- {
- l[a[i]] = i;
- if (i%len == )
- {
- int tmp = i / len;
- for (int j = ; j < n; j++)
- lt[tmp][j] = l[j];
- }
- }
- int q;
- scanf("%d", &q);
- int ans = ;
- for (; q; q--)
- {
- int L, R;
- scanf("%d%d", &L, &R);
- L--;
- int kl = L / len, kr = R / len;
- ans = ;
- if (kl == kr)
- {
- tot++;
- for (int i = L; i <= R; i++)
- {
- if (lb[a[i]] != tot)
- {
- lb[a[i]] = tot;
- l[a[i]] = i;
- }
- ans = max(ans, i - l[a[i]]);
- }
- }
- else
- {
- ans = answer[kl + ][kr];
- tot++;
- int tmp = min(n, (kl + )*len);
- for (int i = L; i < tmp; i++)
- {
- if (lb[a[i]] != tot)
- {
- lb[a[i]] = tot;
- l[a[i]] = i;
- ans = max(ans, rt[kr - ][a[i]] - i);
- }
- }
- tmp = min(R + , n);
- for (int i = kr*len; i < tmp; i++)
- {
- if (lb[a[i]] != tot)
- {
- if (lt[kl + ][a[i]] != -) ans = max(ans, i - lt[kl + ][a[i]]);
- }
- else ans = max(ans, i - l[a[i]]);
- }
- }
- printf("%d\n", ans);
- }
- /*int q;
- for (scanf("%d", &q); q; q--)
- {
- // if (q % 5000 == 0) cerr << q << endl;
- int L, R;
- tot++;
- scanf("%d%d", &L, &R);
- int ans = 0;
- for (int i = L - 1; i <= R; i++)
- {
- if (lb[a[i]] != tot)
- {
- lb[a[i]] = tot;
- l[a[i]] = i;
- }
- ans = max(ans, i - l[a[i]]);
- }
- printf("%d\n", ans);
- }*/
- /*for (int i = 0; i <= n; i++)
- {
- if (lb[a[i]] == 0)
- {
- lb[a[i]] = 1;
- l[a[i]] = i;
- d[i] = n + 1;
- }
- else
- {
- r[l[a[i]]] = i;
- d[i] = i - l[a[i]];
- l[a[i]] = i;
- }
- }
- root[0] = build(1, n);
- for (int i = 1; i <= n; i++)
- {
- if (r[i-1] != 0) root[i] = change(root[i - 1], 1, n, r[i-1]);
- else root[i] = root[i - 1];
- }
- int q;
- for (scanf("%d", &q); q; q--)
- {
- int L, R;
- scanf("%d%d", &L, &R);
- L--;
- int ans = n + 1;
- check(root[L], 1, n, R, ans);
- if (ans == n + 1) printf("-1\n"); else printf("%d\n", ans);
- }*/
- /* int q;
- int tans = 0;
- for (scanf("%d", &q); q; q--)
- {
- int L, R;
- scanf("%d%d", &L, &R);
- L--;
- tot++;
- int ans = n + 1;
- for (int i = L; i <= R; i++)
- {
- if (lb[a[i]] != tot)
- {
- lb[a[i]] = tot;
- l[a[i]] = i;
- }
- else
- {
- ans = min(ans, i - l[a[i]]);
- l[a[i]] = i;
- }
- }
- if (ans == n + 1) printf("-1\n"); else {
- printf("%d\n", ans);
- tans = max(tans, ans);
- }
- }
- cerr << clock()-ti << " "<<tans<<" "<<A<<" "<<B<<endl;*/
- return ;
- }
std
清北考前刷题day6下午好的更多相关文章
- 清北考前刷题day1下午好
水题(water) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每张牌长和宽 ...
- 清北考前刷题da7下午好
三向城 /* 原图一定是一棵完全二叉树. 根节点是x,左节点是x*2,右节点是x*2+1 转化为二进制往左右走就很明显了. */ #include<iostream> #include&l ...
- 清北考前刷题day3下午好
/* 可以并查集维护 可以发现,某个联通快出现大于等于2个环,一定无法分配. 有解要么一个环,要么没有环. 一个环时答案等于点数乘2(顺时针或逆时针). 没有环是树,对于一个n个点的树,方案一定有n种 ...
- 清北考前刷题da5下午好
/* (4,1)*(3,1)*(2,1)的话1变成2然后一直是2 2变成1然后变成3 3变成1然后变成4 4变成1 */ #include<iostream> #include<cs ...
- 清北考前刷题day4下午好
/* 辗转相除,每次计算多出现了几个数. */ #include<iostream> #include<cstdio> #include<cstring> #inc ...
- 清北考前刷题day2下午好
#include<iostream> #include<cstdio> #include<cstring> #include<stack> #defin ...
- 清北考前刷题day6早安
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- 清北考前刷题day4早安
LI /* 没有考虑次大值有大于一个的情况 */ #include<iostream> #include<cstdio> #include<cstring> # ...
- 清北考前刷题day7早安
随机推荐
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
1100 - Again Array Queries ...
- String replaceAll-正则匹配-截取以指定字符开头,以指定字符结尾的字符串
scala代码块 截取以某个字符开头,以某个字符结尾的字符串 def main(args: Array[String]): Unit = { val s = "{{a61,a2,a3},{b ...
- j简单的分类实现-K近邻
dataSetSize=dataSet.shape[0] voteIlabel=labels[sortedDistIndicies[i]] ...
- zoj4710暴力
#include<stdio.h> #include<string.h> #define N 110 int map[N][N]; int main() { int n,m,k ...
- hdu 1075 字典树
#include<stdio.h> #include<iostream> struct node { int num,i; node *a[27]; char s[20];// ...
- openjudge7624 山区建小学
描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为di(为正整数),其中,0 < i < ...
- 【天道酬勤】 腾讯、百度、网易游戏、华为Offer及笔经面经(转)
应届生上泡了两年,一直都是下资料,下笔试题,面试题.一直都在感谢那些默默付出的人.写这个帖子花了我两 个夜晚的时间,不是为了炫耀,只是为了能给那些“迷惘”的学弟学妹,一点点建议而已.大家何必那么认真, ...
- 【编程大系】Java资源汇总
1.学习资料: 1)Spring Boot 那些事:https://www.w3cschool.cn/springboot/ 对应的 gitHub代码: https://github.com/Jeff ...
- 学习日常笔记<day09>Http协议
1 Http协议入门 1.1 什么是http协议 http协议: 对浏览器客户端 和 服务器端 之间数据传输的格式规范 1.2 查看http协议的工具 1)使用火狐的firebug插件(右键-> ...
- Java电商项目-6.实现门户首页数据展示_Redis数据缓存
目录 项目的Github地址 需求介绍 搭建Redis集群环境 下面先描述单机版redis的安装 下面将进行Redis3主3从集群环境搭建 基于SOA架构, 创建门户ashop-portal-web门 ...