NOIP做题练习(day2)
A - Reign
题解
最大子段和+\(DP\)。
预处理两个数组:
- \(p[i]\)表示 \(i\) 之前的最大子段和。
- \(l[i]\)表示 \(i\) 之后的最大子段和。
最后直接输出即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#define int long long
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int t, n, m, k, ans, a[100003], p[100003], l[100003];
signed main()
{
//File("REIGN");
t = gi();
while (t--)
{
n = gi(), k = gi();
for (itn i = 1; i <= n; i+=1) a[i] = gi();
memset(p, 0, sizeof(p)); memset(l, 0, sizeof(l));
ans = -0x7fffffff;
for (itn i = 1; i <= n; i+=1)
{
if (p[i - 1] < 0) p[i] = a[i];
else p[i] = p[i - 1] + a[i];
}
p[0] = -0x7fffffff;
for (itn i = 1; i <= n; i+=1) p[i] = max(p[i], p[i - 1]);
for (int i = n; i >= 1; i-=1)
{
if (l[i + 1] < 0) l[i] = a[i];
else l[i] = l[i + 1] + a[i];
}
l[n + 1] = -0x7fffffff;
for (itn i = n; i >= 1; i-=1) l[i] = max(l[i], l[i + 1]);
for (int i = 1; i < n - k; i+=1) ans = max(ans, p[i] + l[i + k + 1]);
printf("%lld\n", ans);
}
return 0;
}
C - Strongly Connected City
题意简述
给定\(n\)条水平的单向街道和\(m\)条竖直单向的街道,其交点共有\(n \times m\)个,问这些节点是否都能互相到达。
\(2 \leq n,m \leq 20\)
题解
只需要判断最外圈是不是一个环即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <string>
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
bool fl = false;
int n, m, a[25][25][2], tot, vis[25][25];
char s[2][25];
int main()
{
n = gi(), m = gi();
string s[3];
cin >> s[1];
if (s[1][0] == s[1][n - 1]) {puts("NO"); return 0;}
cin >> s[2];
if (s[1][0] == '<' && s[2][0] == '^') {puts("NO"); return 0;}
if (s[1][0] == '>' && s[2][m - 1] == '^') {puts("NO"); return 0;}
if (s[1][n - 1] == '<' && s[2][0] == 'v') {puts("NO"); return 0;}
if (s[1][n - 1] == '>' && s[2][m - 1] == 'v') {puts("NO"); return 0;}
puts("YES");
return 0;
}
F - Chef and Digit Jumps
题解
\(BFS\)裸题。
有一个优化:让每一个点在队列中只会出现一次,优化时间复杂度。
注意一些细节。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <queue>
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
char s[100003];
int n, m, len, p, ans, sum, b[1000003], vis[100003];
vector <int> a[15];
queue <int> q;
int main()
{
//File("DIGJUMP");
scanf("%s", s + 1);
len = strlen(s + 1);
for (int i = 1; i <= len; i+=1)
{
a[s[i] - '0'].push_back(i);
}
q.push(1); b[1] = 0;
while (!q.empty())
{
int x = q.front(); q.pop();
if (x == len) break;
int y = s[x] - '0';
if (!vis[y])
{
vis[s[x] - '0'] = 1;
for (int i = 0; i < a[y].size(); i+=1)
{
int z = a[y][i];
if (z != x && b[z] == 0)
{
b[z] = b[x] + 1;
q.push(z);
}
}
}
if (x - 1 >= 1 && b[x - 1] == 0) {b[x - 1] = b[x] + 1; q.push(x - 1);}
if (x + 1 <= len && b[x + 1] == 0) {b[x + 1] = b[x] + 1; q.push(x + 1);}
}
printf("%d\n", b[len]);
return 0;
}
总结
这次做题做得很不理想。
\(T3\)时间复杂度算错浪费了很多时间。
\(T1\)想了很久才想到正解。
要多多练习,才能有提高啊!
NOIP做题练习(day2)的更多相关文章
- noip做题记录+挑战一句话题解?
因为灵巧实在太弱辽不得不做点noip续下命QQAQQQ 2018 积木大赛/铺设道路 傻逼原题? 然后傻逼的我居然检查了半天是不是有陷阱最后花了差不多一个小时才做掉我做过的原题...真的傻逼了我:( ...
- NOIP做题练习(day1)
A - Xenny and Alternating Tasks 题面 题解 枚举第一天是谁做,将两个答案取\(min\)即可. 代码 #include <iostream> #includ ...
- $NOIp$做题记录
虽然去年做了挺多了也写了篇一句话题解了但一年过去也忘得差不多了$kk$ 所以重新来整理下$kk$ $2018(4/6$ [X]积木大赛 大概讲下$O(n)$的数学方法. 我是从分治类比来的$QwQ$. ...
- NOIP做题练习(day4)
A - 同花顺 题面 题解 30分做法 爆搜即可. 60分做法 去重+贪心. 100分做法 去重+贪心后,我们要寻找一段符合条件的最长同花上升子序列 \(L\),\(n-L\) 即为所求的答案. 首先 ...
- NOIP做题练习(day5)
A - 中位数图 题面 题解 先找出题意中的\(b\)所在的位置. 再以这个位置为中心,向右\(for\)一遍有多少个大于/小于该数的数 大于就\(++cs\) 小于就\(--cs\). 因为这个数是 ...
- NOIP做题练习(day3)
A - 军队 问题描述 给定一个有 \(n\) 个队伍的人组成的序列,第 \(i\) 个队伍 \(i\) 有 \(s[i]\)个人组成,一个 \(l\) 到 \(r\)的子序列是合法的,当且仅当\(( ...
- NOIP初赛:完善程序做题技巧
最近写的文章好像还很多的.那么今天我们来讨论NOIP初赛的题型--完善程序.完善程序相对是比较难的题目了.全卷100分,完善程序占了大概26分,占比非常大.如果和英语考试试卷做比较,相当于首字母填空( ...
- [日记&做题记录]-Noip2016提高组复赛 倒数十天
写这篇博客的时候有点激动 为了让自己不颓 还是写写日记 存存模板 Nov.8 2016 今天早上买了两个蛋挞 吃了一个 然后就做数论(前天晚上还是想放弃数论 但是昨天被数论虐了 woc noip模拟赛 ...
- CodeM美团点评编程大赛复赛 做题感悟&题解
[T1] [简要题意] 长度为N的括号序列,随机确定括号的方向:对于一个已确定的序列,每次消除相邻的左右括号(右左不行),消除后可以进一步合并和消除直到不能消为止.求剩下的括号的期望.\(N \l ...
随机推荐
- 关于Hosts与network的异同之处
1.hosts文件,路径:/etc/hosts,此文间是在网络上使用的,用于解析计算机名称和IP地址的映射关系,功能相当于windows下面的c:\windows\system32\drivers\e ...
- 【剑指Offer】02、替换空格
题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 题解:StringBuffer ...
- Android Studio 安装问题。
安装时,这里要选Cancel 安装AS时因为选择了Setup Proxy, 后面带来很多问题. --------------------------------------------- 参考这个安装 ...
- Scala之Option: Some None
Option类型本身没有实现,而是依赖两个子类型提供具体实习那:Some和None.Some是iyge类型参数化的单元素集合,None是一个空集合. ----<scala学习手册>P119
- Leetcode 995. K 连续位的最小翻转次数
题目: 在仅包含 0 和 1 的数组 A 中,一次 K 位翻转包括选择一个长度为 K 的(连续)子数组,同时将子数组中的每个 0 更改为 1,而每个 1 更改为 0. 返回所需的 K 位翻转的次数,以 ...
- Python论做游戏外挂,Python输过谁?
玩过电脑游戏的同学对于外挂肯定不陌生,但是你在用外挂的时候有没有想过如何做一个外挂呢? 我打开了4399小游戏网,点开了一个不知名的游戏,唔,做寿司的,有材料在一边,客人过来后说出他们的要求,你按照菜 ...
- koa文档笔记
请求 get ctx.request.query // 查询对象 ctx.request.querystring // 查询字符串 ctx.query // 查询对象 ctx.querystring ...
- C# WPF过渡效果实现(C# WPF Material Design UI: Transitions)
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- Query的选择器
Query的选择器 一. 基本选择器 1. ID选择器 ID选择器#id就是利用DOM元素的id属性值来筛选匹配的元素,并以iQuery包装集的形式返回给对象. ...
- canvas特效-文字粒子
具体的效果 https://www.ui.cn/detail/393461.html 实现类似的动效 要求: 1.文字内容可以自定义 2.粒子的运动轨迹 是曲线,返回是按照 原运动轨迹 3.粒子 堆叠 ...