Codeforces Round #849 (Div. 4)
A. Codeforces Checking
题意
每个案例给一个字符,如果在 ”codeforces“ 中出现过,输出 YES,否则输出 NO
code
/**
* @author :Changersh
* @date : 2023/2/3 22:37
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static boolean[] a = new boolean[26];
public static void main(String[] args) {
String s = "codeforces";
for (int i = 0; i < s.length(); i++)
a[s.charAt(i) - 'a'] = true;
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
char t = sc.next().charAt(0);
if (a[t - 'a']) out.println("YES");
else out.println("NO");
}
out.close();
}
static class FastScanner{
// 看看有没有溢出,是否要用 long
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
B. Following Directions
题意
每个案例给一串字符串,包含 U、D、L、R,是上下左右四个方向,从 (0, 0) 开始,问是否经过 (1, 1)
code
直接模拟即可
/**
* @author :Changersh
* @date : 2023/2/3 22:43
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int N = 55, n, T;
public static void main(String[] args) {
T = sc.nextInt();
while (T-- > 0) {
out.println(solve() ? "YES" : "NO");
}
out.close();
}
private static boolean solve() {
n = sc.nextInt();
char[] c = sc.next().toCharArray();
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
if (c[i] == 'U') x++;
else if (c[i] == 'D') x--;
else if (c[i] == 'L') y--;
else y++;
if (x == 1 && y == 1) return true;
}
return false;
}
static class FastScanner {
// 看看有没有溢出,是否要用 long
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st = new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public boolean hasNext() {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null) return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
C. Prepend and Append
题意
给你一串由 0 1 组成的字符串,如果第一个和最后一个是 0 1或者 1 0 ,可以消掉,这种操作可以进行任意次,问,任意次操作后,字符串剩下的最短长度是多少?
code
双指针判断第一个和最后一个字符,模拟即可
/**
* @author :Changersh
* @date : 2023/2/3 22:51
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int T;
public static void main(String[] args) {
T = sc.nextInt();
while (T-- > 0)
solve();
out.close();
}
private static void solve() {
int n = sc.nextInt();
char[] c = sc.next().toCharArray();
int l = 0, r = n - 1;
while (l < r) {
if ((c[l] == '0' && c[r] == '1') || (c[l] == '1' && c[r] == '0')) {
l++;
r--;
}
else break;
}
out.println(r - l + 1);
}
static class FastScanner{
// 看看有没有溢出,是否要用 long
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
D. Distinct Split
题意
每个案例给定一个由小写字母组成的字符串,求将字符串从中间劈开,分成两个字串中,出现的不同的字符数的和的最大值?
code
暴力写会tle,所以先预处理一下前缀和后缀,前缀是到 第 i 个字符截止,前面 i 个字符共出现多少个不同的字符
后缀:从
/**
* @author :Changersh
* @date : 2023/2/3 23:04
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int T, n;
private static char[] c;
public static void main(String[] args) {
T = sc.nextInt();
while (T-- > 0)
solve();
out.close();
}
private static void solve() {
n = sc.nextInt();
c = sc.next().toCharArray();
int ans = 0;
int[] l = new int[n + 2];
int[] r = new int[n + 2];
get(l, r);
for (int i = 0; i < n; i++) {
ans = Math.max(ans, l[i + 1] + r[i + 2]);
}
out.println(ans);
}
private static void get(int[] l, int[] r) {
int ans = 0;
HashSet<Character> vis = new HashSet<>();
for (int i = 0; i < n; i++) {
if (!vis.contains(c[i])) {
vis.add(c[i]);
ans++;
}
l[i + 1] = ans;
}
vis.clear();
ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (!vis.contains(c[i])) {
vis.add(c[i]);
ans++;
}
r[i + 1] = ans;
}
}
static class FastScanner {
// 看看有没有溢出,是否要用 long
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st = new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public boolean hasNext() {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null) return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
E. Negatives and Positives
题意
给定一串数字,你可以进行任意次以下的操作:
选择两个不同的数字,变成相反数
求,进行任意次之后,数组数字和的最大值是多少?
code
数字有正数、负数、0
翻转的时候,最佳的方法是把所有的负数都变成正数,是最佳情况
- 负数个数是偶数,完美,全部都可以翻转成正数
- 负数个数是奇数:
1. 有 0,依然完美
2. 找绝对值最小的,取负即可
将第二种情况的两种小情况和一
遍历数组的时候,统计负数个数,并且把负数都翻转,求和
如果是偶数,返回答案
如果是奇数,说明不能完美翻转,数组排序,得到最小的数字,无论是正数还是负数。抑或是 0,减两次即可。
因为最小的如果是 负数翻转的,减两次相当于没有翻转
如果是正数,相当于把 落单的负数和绝对值小于它的正数翻转,和变大
如果是 0,也是完美的情况
/**
* @author :Changersh
* @date : 2023/2/4 9:20
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int T, n, N = 200010;
private static int[] a;
public static void main(String[] args) {
T = sc.nextInt();
while (T-- > 0)
solve();
out.close();
}
private static void solve() {
long sum = 0;
int cnt = 0;
n = sc.nextInt();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
if (a[i] < 0) {
cnt++;
a[i] = -a[i];
}
sum += a[i];
}
if ((cnt & 1) == 1) {
Arrays.sort(a);
sum -= 2 * a[0];
}
out.println(sum);
}
static class FastScanner{
// 看看有没有溢出,是否要用 long
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
G1. Teleporters (Easy Version)
题意
有 0~n,n + 1个点,我们可以从 1 ~ n 号点用 传送器瞬间移动到 0号点,花费 a[i] 块钱
从一个点移动到隔壁,花费 1 块钱
给你一串 传送的花费 和现在总共有的钱 ,问从 0 出发最多能用几次传送
由题意得,传送的使用条件是:
- 先花费 i 块钱,从 0 走到 i
- 花费 a[i] 传送
code
Codeforces Round #849 (Div. 4)的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- git ignore忽略规则
目录 Git 忽略文件提交的方法 Git 忽略规则 Git 忽略规则优先级 Git 忽略规则匹配语法 常用匹配示例 多级目录忽略规则设置 .gitignore规则不生效 参考文章 Git 忽略文件提交 ...
- mindxdl--common--web_cert_utils.go
// Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.// Package common this file ...
- Go语言核心36讲26
你好,我是郝林.今天我分享的主题是测试的基本规则和流程的(下)篇. Go语言是一门很重视程序测试的编程语言,所以在上一篇中,我与你再三强调了程序测试的重要性,同时,也介绍了关于go test命令的基本 ...
- 已经有 MESI 协议,为什么还需要 volatile 关键字?
本文已收录到 GitHub · AndroidFamily,有 Android 进阶知识体系,欢迎 Star.技术和职场问题,请关注公众号 [彭旭锐] 进 Android 面试交流群. 前言 大家好 ...
- Karmada跨集群优雅故障迁移特性解析
摘要:在 Karmada 最新版本 v1.3中,跨集群故障迁移特性支持优雅故障迁移,确保迁移过程足够平滑. 本文分享自华为云社区<Karmada跨集群优雅故障迁移特性解析>,作者:Karm ...
- odoo关于计算字段store=True时导致的安装/更新时间较长问题的解决方案
Odoo安装/更新模块原理 Odoo每次安装/更新模块时,会进行以下几步处理: 1.判断是否需要创建表,如果需要创建且表不存在,则进行表的创建(不进行字段的创建): 2.获取该表中已经存在的字段: 3 ...
- Java反射与安全问题
1.Java反射机制 Java反射机制是指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的 ...
- Docker原理(图解+秒懂+史上最全)
背景:下一个视频版本,从架构师视角,尼恩为大家打造高可用.高并发中间件的原理与实操. 目标:通过视频和博客的方式,为各位潜力架构师,彻底介绍清楚架构师必须掌握的高可用.高并发环境,包括但不限于: 高可 ...
- Spring面试点汇总
Spring面试点汇总 我们会在这里介绍我所涉及到的Spring相关的面试点内容,本篇内容持续更新 我们会介绍下述Spring的相关面试点: Spring refresh Spring bean Sp ...
- 从面试题入手,畅谈 Vue 3 性能优化
前言 今年又是一个非常寒冷的冬天,很多公司都开始人员精简.市场从来不缺前端,但对高级前端的需求还是特别强烈的.一些大厂的面试官为了区分候选人对前端领域能力的深度,经常会在面试过程中考察一些前端框架的源 ...