【atcoder beginner 308E - MEX】
- 前缀和
- 二分查找
- 打表枚举
- 代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.List; public class Main { public static Integer[] dp(int[] arr, String s,int t,char ch) {
List<Integer> ans = new ArrayList<>();
if (arr[0] == t && s.charAt(0) == ch) {
ans.add(1);
} else {
ans.add(0);
}
for (int i = 1; i < arr.length; i++) {
if (arr[i] == t && s.charAt(i) == ch) {
ans.add(ans.get(i - 1) + 1);
} else {
ans.add(ans.get(i - 1));
}
}
return ans.toArray(new Integer[ans.size()]);
} public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
in.nextToken();
arr[i] = (int) in.nval;
}
in.nextToken();
String s = in.sval;
Integer[] mlist = build(s, 'M');
Integer[] elist = build(s, 'E');
Integer[] xlist = build(s, 'X'); Integer[] hm0 = dp(arr, s,0,'M');
Integer[] hm1 = dp(arr, s,1,'M');
Integer[] hm2 = dp(arr,s, 2,'M'); Integer[] hx0 = dp(arr, s,0,'X');
Integer[] hx1 = dp(arr, s,1,'X');
Integer[] hx2 = dp(arr,s, 2,'X'); long ans = 0;
for (int i = 0; i < elist.length; i++) {
int j = upperlound(mlist, elist[i]);
int p = upperlound(xlist, elist[i]); if (j == 0) {
continue;
}
if (p == xlist.length) {
continue;
}
j = j - 1; int t = mlist[j]; int x0 = hm0[t];
int x1 = hm1[t];
int x2 = hm2[t]; int k = xlist[p]; int y0 = k > 0 ? hx0[n - 1] - hx0[k - 1] : hx0[n - 1];
int y1 = k > 0 ? hx1[n - 1] - hx1[k - 1] : hx1[n - 1];
int y2 = k > 0 ? hx2[n - 1] - hx2[k - 1] : hx2[n - 1]; int z = arr[elist[i]]; if (z == 2) {
ans += 1l*x0*(y0 + y2 + 3 * y1);
ans += 3l*x1*y0;
ans += 1l* x2*y0;
}
else if( z == 1){
ans += 1l*x0*(2*y0+2*y1+3*y2);
ans += 2l*x1*y0;
ans += 3l*x2*y0;
}
else{
ans += 1l*x0*(y0+2*y1+y2);
ans += 1l*x1*(2*y0+2*y1+3*y2);
ans += 1l*x2*(y0+3*y1+y2);
} } System.out.println(ans);
} /**
* @param arr
* @param value
* @return 》 value的第一个坐标位置,如果不存在,那么就是arr.length;
*/
public static int upperlound(Integer[] arr, int value) {
int low = 0;
int high = arr.length;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
} public static Integer[] build(String str, char ch) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
list.add(i);
}
}
return list.toArray(new Integer[list.size()]);
} }
【atcoder beginner 308E - MEX】的更多相关文章
- 【AtCoder Beginner Contest 181】A~F题解
越学越菜系列 于2020.11.2,我绿了(错乱) A - Heavy Rotation 签到题,奇数Black,偶数White. code: #include<bits/stdc++.h> ...
- 【AtCoder Beginner Contest 074 D】Restoring Road Network
[链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...
- 【AtCoder Beginner Contest 074 C】Sugar Water
[链接]h在这里写链接 [题意] 让你在杯子里加糖或加水. (4种操作类型) 糖或水之间有一定关系. 糖和水的总量也有限制. 问你糖水浓度的最大时,糖和糖水的量. [题解] 写个dfs就好. 每次有4 ...
- 【AtCoder Beginner Contest 074 B】Collecting Balls (Easy Version)
[链接]h在这里写链接 [题意] 看懂题目之后就会发现是道大水题. [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h&g ...
- 【AtCoder Beginner Contest 074 A】Bichrome Cells
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...
- 【AtCoder Regular Contest 082】Derangement
[链接]点击打开链接 [题意] 在这里写题意 [题解] 贪心. 连续一块的p[i]==i的话,对答案的贡献就应该为(这个连续块的长度+1)/2; 长度为1的也正确. (也即两两相邻的互换位置.) [错 ...
- 【AtCoder ABC 075 D】Axis-Parallel Rectangle
[链接] 我是链接,点我呀:) [题意] 让你找到一个各边和坐标轴平行的矩形.使得这个矩形包含至少K个点. 且这个矩形的面积最小. [题解] 把所有的"关键点""都找出来 ...
- 【AtCoder ABC 075 C】Bridge
[链接] 我是链接,点我呀:) [题意] 让你求出桥的个数 [题解] 删掉这条边,然后看看1能不能到达其他所有的点就可以了 [代码] #include <bits/stdc++.h> us ...
- 【AtCoder ABC 075 B】Minesweeper
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟,把#换成1 八个方向加一下就好. [代码] #include <bits/stdc++.h> using name ...
- 【AtCoder ABC 075 A】One out of Three
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map轻松搞定 [代码] #include <bits/stdc++.h> using namespace std; ...
随机推荐
- 关于JAVA泛型数组类型擦除引发的问题及解决方案
先看如下一个DEMO示例代码:(其中doBatchGet被子类重写了1次) public abstract class BaseDemoService<T> { public String ...
- 设计模式 - 创建型模式 - 单例模式(C++)
1.前言 单例模式属于创建型模式,保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方 ...
- ORACLE ROLLUP和CUBE介绍
http://blog.csdn.net/wanghai__/article/details/4817920 ------------------ ROLLUP,是GROUP BY子句的一种扩展,可以 ...
- Linux crontab不执行
Linux 系统里面计划任务,crontab 没有如期执行这是运维工作中比较常见的一种故障了. 下面结合最近部署自动脚本不执行问题排查步骤: 1.检查 crontab 服务是否正常 [dmdba@te ...
- win32 - ListView_GetItemPosition的使用
ListView_GetItemPosition : Gets the position of a list-view item 理论上获得桌面图标的正确方法是使用shell项,=> IFold ...
- 使用Gulp压缩静态资源
如果希望对在静态页面中引入的相关资源进行压缩(比如:CSS,JavaScript,图片等),可以使用Gulp实现. 当然,还可以使用其他打包工具,比如:Grunt,Webpack等等. Gulp是什么 ...
- gitee配置SSH公钥
第一步,找个地方打开"git bash",然后输入生成ssh公钥的命令: ssh-keygen -t rsa -C 'your-email' 然后敲四次回车生成公钥: 第二步,输入 ...
- pyqt5中通过pycharm配置designer(win和mac都适用,修改下designer目录路径即可)
安装 pip install PyQt5 -i https://pypi.douban.com/simple pip install PyQt5-tools -i https://pypi.douba ...
- 吐血分享一套iOS底层面试题,真心想帮你!!!
一 选择题(单选/多选) 1. 在LP64下,一个指针的有多少个字节 A: 4 B: 8 C: 16 D: 64 答案B解析: 1个指针8字节 2. 一个实例对象的内存结构存在哪些元素 A:成员变量 ...
- 【API Management】使用 APIM Inbound Policy 来修改Content‐Type Header的值
问题描述 在使用APIM提供API服务管理的场景中,遇见了客户端请求时候发送的请求Header中的Content-Type不满足后台服务器的要求,但是在客户端要求客户修改代码难度较高. 所以面对这样的 ...