NOIP做题练习(day1)
A - Xenny and Alternating Tasks
题解
枚举第一天是谁做,将两个答案取\(min\)即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#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, a[20003], b[20003];
int main()
{
//File("XENTASK");
t = gi();
while (t--)
{
n = gi();
for (int i = 1; i <= n; i+=1) a[i] = gi();
for (itn i = 1; i <= n; i+=1) b[i] = gi();
itn ans = 0, sum = 0;
for (itn j = 1; j <= n; j+=1)
{
if (j & 1) sum = sum + a[j];
else sum = sum + b[j];
}
for (int j = 1; j <= n; j+=1)
{
if (j & 1) ans = ans + b[j];
else ans = ans + a[j];
}
printf("%d\n", min(ans, sum));
}
return 0;
}
B - Bear and Extra Number
题解
将数列排序,遍历数组元素,然后分类讨论:
- 如果是\(a_i = a_{i+1}\) ,那么输出\(a_i\)。
- 如果\(a_{i+1}-a_i>1\) :
- 因为题目说有唯一解,因此\(i\)只能为\(n-1\)或\(1\)。
- 若\(i\)为\(n-1\),则输出\(a_n\)。
- 若\(i\)为\(1\),则输出\(a_1\)。
- 因为题目说有唯一解,因此\(i\)只能为\(n-1\)或\(1\)。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#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, a[100003], ans, sum;
int main()
{
//File("EXTRAN");
t = gi();
while (t--)
{
n = gi();
for (itn i = 1; i <= n; i+=1) a[i] = gi();
sort(a + 1, a + 1 + n);
for (int i = 1; i < n; i+=1)
{
if (a[i + 1] == a[i]) {printf("%d\n", a[i]); break;}
if (a[i + 1] - a[i] > 1)
{
if (i == n - 1) {printf("%d\n", a[n]); break;}
else if (i == 1) {printf("%d\n", a[1]); break;}
}
}
}
return 0;
}
C - Cooking Schedule
题解
二分。
注意\(check\)怎么写。
第一次做时没想到二分,很遗憾。
代码
#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 n, m, t, k, ans, kkk, c[1000003];
char s[1000003];
int CCC(itn x)
{
int u = 0;
for (int i = 1; i <= n; i+=1)
{
if (i % 2 == x)
{
if (s[i] == '0') ++u;
}
else {if (s[i] == '1') ++u;}
}
return u;
}
bool check(int x)
{
if (x != 1)
{
int u = 0;
for (itn i = 1; i <= kkk; i+=1) if (c[i] > x) u = u + c[i] / (x + 1);
return u <= k;
}
else {int u = min(CCC(1), CCC(0)); return u <= k;}
}
signed main()
{
//File("SCHEDULE");
t = gi();
while (t--)
{
int l = 1, r = 0, uuu = 0; kkk = 0;
n = gi(), k = gi();
scanf("%s", s + 1);
for (int i = 1; i <= n; i+=1)
{
if (s[i] != s[i + 1])
{
c[++kkk] = i - uuu; uuu = i; r = max(r, c[kkk]);
}
}
while (l < r)
{
int mid = (l + r) >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
printf("%lld\n", r);
}
return 0;
}
D - Subtree Removal
题解
简单树形\(DP\)。
看似很难,实际代码很短。
计算出每棵子树的大小,与\(-X\)取\(max\)即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <vector>
#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, x, fa[100003], ans, sum, k, b[100003], sz[100003];
vector <int> e[100003];
int dfs(int u, int fa)
{
int o = b[u];
for (int i = e[u].size() - 1; ~i; i-=1)
{
int v = e[u][i];
if (v == fa) continue;
o = o + dfs(v, u);
}
return max(o, -x);
}
signed main()
{
//File("SUBREM");
t = gi();
while (t--)
{
n = gi(), x = gi();
int sum = 0;
for (itn i = 1; i <= n; i+=1) b[i] = gi(), sz[i] = b[i], sum = sum + b[i], e[i].clear();
for (itn i = 1; i < n; i+=1) {int u = gi(), v = gi(); e[u].push_back(v), e[v].push_back(u);}
printf("%lld\n", dfs(1, -1));
}
return 0;
}
E - Dish Owner
题解
并查集简单题。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#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;
}
itn t, n, s[10003], fl, x, y, Q;
itn a[10003], fa[10003];
int getf(itn u) {if (u == fa[u]) return u; return fa[u] = getf(fa[u]);}
inline void unionn(int u, int v) {
if (a[u] > a[v])
{
fa[v] = u;
}
else if (a[v] > a[u])
{
fa[u] = v;
}
}
int main()
{
//File("DISHOWN");
t = gi();
while (t--)
{
n = gi();
for (itn i = 1; i <= n; i+=1) s[i] = gi(), a[i] = s[i], fa[i] = i;
Q = gi();
for (itn i = 1; i <= Q; i+=1)
{
fl = gi();
if (fl)
{
x = gi();
printf("%d\n", getf(x));
}
else
{
x = gi(), y = gi();
int X = getf(x), Y = getf(y);
if (X == Y) puts("Invalid query!");
else unionn(X, Y);
}
}
}
return 0;
}
F - Triplets
题解
数学题。
大暴力很好写,考虑如何优化。
预处理出\(a\)、\(c\)序列的前缀和。
逐一枚举\(b\)序列上的数,找出\(a\)、\(c\)序列中比当前枚举到的数小的数。
计算出其对答案的贡献即可。
代码
#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;
}
const int mod = 1000000007;
itn t, n, p, q, r, a[100003], b[100003], c[100003], x, y, z;
itn s1 = 1, s2 = 1, s3 = 1;
signed main()
{
//File("SUMQ");
t = gi();
while (t--)
{
p = gi(), q = gi(), r = gi();
for (int i = 1; i <= p; i+=1) a[i] = gi();
for (int i = 1; i <= q; i+=1) b[i] = gi();
for (itn i = 1; i <= r; i+=1) c[i] = gi();
sort(a + 1, a + 1 + p); sort(b + 1, b + 1 + q); sort(c + 1, c + 1 + r);
int p1 = 1, p2 = 1, c1 = 0, c2 = 0, s1 = 0, s2 = 0, ans = 0;
for (int i = 1; i <= q; i+=1)
{
while (p1 <= p && a[p1] <= b[i])
{
s1 = (s1 + a[p1]) % mod;
++c1, ++p1;
}
while (p2 <= r && c[p2] <= b[i])
{
s2 = (s2 + c[p2]) % mod;
++c2, ++p2;
}
ans = (ans % mod + c1 * c2 % mod * b[i] % mod * b[i] % mod) % mod;
ans = (ans % mod + b[i] * (s1 * c2 % mod + s2 * c1 % mod) % mod) % mod;
ans = (ans % mod + s1 * s2 % mod) % mod;
}
printf("%lld\n", ans % mod);
}
return 0;
}
总结
这次练习第一次做的时候做得一般般。
做题的策略要更好一点。
细节地方要注意。
NOIP做题练习(day1)的更多相关文章
- noip做题记录+挑战一句话题解?
因为灵巧实在太弱辽不得不做点noip续下命QQAQQQ 2018 积木大赛/铺设道路 傻逼原题? 然后傻逼的我居然检查了半天是不是有陷阱最后花了差不多一个小时才做掉我做过的原题...真的傻逼了我:( ...
- NOIP做题练习(day2)
A - Reign 题面 题解 最大子段和+\(DP\). 预处理两个数组: \(p[i]\)表示 \(i\) 之前的最大子段和. \(l[i]\)表示 \(i\) 之后的最大子段和. 最后直接输出即 ...
- $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 ...
随机推荐
- Java(四)输出和输入函数
介绍一下Java里简单常用的输入输出方法. Java的输出函数很简单,直接调用System类的out对象的print函数即可. 代码: System.out.print(a);//输出变量a的值 Sy ...
- openlayers轨迹播放
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java集合框架备忘
List,Set,Map三者的区别? List(对付顺序的好帮手): List接口存储一组不唯一(可以有多个元素引用相同的对象),有序的对象 Set(注重独一无二的性质): 不允许重复的集合.不会有多 ...
- Java String类型转换成Date日期类型
插入数据库时,存入当前日期,需要格式转换 import java.text.SimpleDateFormat; formatter = new SimpleDateFormat( "yyyy ...
- python&mysql
第一种方法,使用 pymysql库 import pymysql.cursors # Connect to the database connection = pymysql.connect(host ...
- 零基础入门 Kubernetes,你需要知道这些
Kubernetes是什么? 大概很多人对此都有疑问,不过在容器领域,Kubernetes却无人不晓. 阿里.字节跳动.腾讯.百度等中国互联网行业巨擘们,近年来都在深耕容器领域,而Kubernetes ...
- 前端Yslow的23个优化原则
前端Yslow的23个优化原则 最常遇见的前端优化问题. Yslow是雅虎开发的基于网页性能分析浏览器插件,可以检测出网页的具体性能值,并且有著名的Yslow 23条优化规则,这23条,就够我们玩的了 ...
- 微信小程序中如何上传图片
本篇文章给大家带来的内容是关于微信小程序中如何上传图片(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 一.wxml文件 <text>上传图片</text ...
- 使用VSCode创建简单的Razor Webapp--1.入门
1.下载vscode,安装dotnet core sdk 在cmd中使用命令dotnet --version可以查看当前安装的版本 2.打开vscode,设置语言和扩展 在最左边的工具栏,点击最下面的 ...
- [TJOI2015] 线性代数 - 最大权闭合子图
展开 \(D=(AB-C)A^T\\ =\sum_{i=1}^n(\sum_{j=1}^na_jb_{j,i}-c_i)a_i\\ =\sum_{i=1}^n\sum_{j=1}^na_ia_jb_{ ...