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 ...
随机推荐
- Spark学习之路 (九)SparkCore的调优之数据倾斜调优[转]
调优概述 有的时候,我们可能会遇到大数据计算中一个最棘手的问题--数据倾斜,此时Spark作业的性能会比期望差很多.数据倾斜调优,就是使用各种技术方案解决不同类型的数据倾斜问题,以保证Spark作业的 ...
- c#在类里不能使用Response解决方法
response对应的类是HttpResponse, 在System.Web 命名字间里, 如果你在类中要使用 Response 的话, 需要使用System.Web.HttpConte ...
- PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) (排序)
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- BZOJ4668 冷战(LCT维护最小生成树)
BZOJ4668 冷战(LCT维护最小生成树) 题面 自己找去 HINT 这道题就是动态加边,然后查询u,v两点最早什么时候联通,强制在线.思考一下,最早什么时候联通不就等同于维护最小生成树吗(把这条 ...
- 函数节流-歪说js
歪谈js 起因: 夜深人静,月朗星稀.'window.onresize 事件' 与 '浏览器'在大战300回合,console.log('1') 1s 十次,然后就结束了,一个悲伤的故事. 事实证明太 ...
- pytest参数化 parametrize
pytest.mark.parametrize装饰器可以实现测试用例参数化 parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of test ...
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(5.23)-- Format NVM command
5.23 Format NVM command - NVM Command Set Specific Format NVM命令用于低级格式化NVM媒介.这个命令被host主机使用,来变更LBA数据大小 ...
- [CF1304F] Animal Observation - dp,单调队列
设 \(f[i][j]\) 为第 \(i\) 天在第 \(j\) 个位置放置的最大值,设 \(s[i][j]\) 是第 \(i\) 行的前缀和,则 \[ \begin{align} f[i][j] & ...
- [HNOI2003] 消防局的设立 - 树形dp
仍然是点覆盖集问题,但覆盖半径变成了\(2\) 延续上一题的思路,只是式子更加复杂了 想体验一下min_element大法于是不想优化了 #include <bits/stdc++.h> ...
- I+Me=完整的自我
这是这个系列的第二篇文章,如同第一篇一样,这篇文章会在24小时后删除. 之所以如此极端,因为我自认为这篇文章很有价值,不以这种方式,大家即使看了,也只会一带而过,不会真的汲取到营养. 这篇文章涉及的关 ...