2016年NOIP普及组复赛题解
题目涉及算法:
- 买铅笔:入门题;
- 回文日期:枚举;
- 海港:双指针;
- 魔法阵;数学推理。
买铅笔
题目链接:https://www.luogu.org/problem/P1909
设至少要买 \(num\) 只笔,且对于每只钱币,设它的价格为 \(a\) ,笔数为 \(b\) ,则花费为最大的那个 \(\lceil \frac{num}{a} \rceil \times b\) 。
实现代码如下:
#include <bits/stdc++.h>
using namespace std;
int num, a, b, ans = INT_MAX;
int main() {
cin >> num;
for (int i = 0; i < 3; i ++) {
cin >> a >> b;
ans = min(ans, (num+a-1)/a*b);
}
cout << ans << endl;
return 0;
}
回文日期
题目链接:https://www.luogu.org/problem/P2010
使用枚举解决,实现代码如下:
#include <bits/stdc++.h>
using namespace std;
string start_date, end_date;
int cnt;
void solve(int year) {
int a[8];
a[0] = year / 1000;
a[1] = year / 100 % 10;
a[2] = year / 10 % 10;
a[3] = year % 10;
for (int i = 0; i < 4; i ++) a[7-i] = a[i];
int month = a[4]*10 + a[5];
int day = a[6]*10 + a[7];
if (month < 1 || month > 12) return;
int dd = 31;
if (month<8 && month%2==0 || month>7 && month%2!=0) dd = 30;
else if (month == 2) {
if (year%400==0 || year%4==0&&year%100!=0) dd = 29;
else dd = 28;
}
if (day < 1 || day > dd) return;
string s = "";
for (int i = 0; i < 8; i ++) {
s += (char) ('0' + a[i]);
}
if (s >= start_date && s <= end_date) cnt ++;
}
int main() {
cin >>start_date >> end_date;
for (int i = 0; i < 10000; i ++) solve(i);
cout << cnt << endl;
return 0;
}
海港
题目链接:https://www.luogu.org/problem/P2058
本题涉及算法:双指针法。
我们令 \(c[i]\) 表示当前时刻在海港内国别为 \(i\) 的人数,令 \(cnt\) 表示当前时刻海港内有多少个不同国别的人。
我们一开始另 \(j = 0\) ,然后从 \(0\) 到 \(n-1\) 遍历 \(i\) :
对于变量 \(i\) ,首先遍历它里面所有国别 \(x\) ,\(c[x] ++\) ,如果此时 \(c[x] = 1\) ,说明这个国别是第一次有,令 \(cnt ++\) ;
只要满足 \(t[j] \le t[i] - 86400\) ,我们就执行如下操作:
遍历 \(t[j]\) 时刻所有旅客的国别 \(x\) ,\(c[x] --\) ,如果此时 \(c[x] = 0\) ,说明这个国别的旅客都没有了,令 \(cnt --\);然后就是 \(j++\) ,直到 \(t[j] \gt t[i] - 86400\) 。
实现代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int DELTA = 86400;
int n, a, t[maxn], k[maxn], c[maxn], cnt;
vector<int> x[maxn];
void my_add(int id) { // 增加编号为id的信息
for (int i = 0; i < k[id]; i ++) {
int a = x[id][i];
c[a] ++;
if (c[a] == 1) cnt ++;
}
}
void my_del(int id) { // 删除编号为id的信息
for (int i = 0; i < k[id]; i ++) {
int a = x[id][i];
c[a] --;
if (c[a] == 0) cnt --;
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i ++) {
cin >> t[i] >> k[i];
for (int j = 0; j < k[i]; j ++) {
cin >> a;
x[i].push_back(a);
}
}
for (int i = 0, j = 0; i < n; i ++) {
my_add(i);
while (t[j] <= t[i] - DELTA) {
my_del(j ++);
}
cout << cnt << endl;
}
return 0;
}
魔法阵
题目链接:https://www.luogu.org/problem/P2119
题解:本着不重复造轮子的思想,请转 yhf2000 大神的博文:https://www.luogu.org/blog/yhf/solution-p2119
实现代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 15010, maxm = 40040;
int n, m, a[maxn], b[maxn], c[maxn], d[maxn], val[maxm], cnt[maxn];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i ++) {
scanf("%d", &val[i]);
cnt[ val[i] ] ++;
}
for (int i = 1; 2+9*i <= n; i ++) {
int tmp = 0;
for (int j = 2+9*i; j <= n; j ++) {
tmp += cnt[j-1-9*i] * cnt[j-1-7*i];
c[j-i] += cnt[j] * tmp;
d[j] += cnt[j-i] * tmp;
}
tmp = 0;
for (int j = n-1-9*i; j >= 1; j --) {
tmp += cnt[j+1+8*i] * cnt[j+1+9*i];
a[j] += cnt[j+2*i] * tmp;
b[j+2*i] += cnt[j] * tmp;
}
}
for (int i = 0; i < m; i ++)
printf("%d %d %d %d\n", a[ val[i] ], b[ val[i] ], c[ val[i] ], d[ val[i] ]);
return 0;
}
作者:zifeiy
2016年NOIP普及组复赛题解的更多相关文章
- 2010年NOIP普及组复赛题解
题目及涉及的算法: 数字统计:入门题: 接水问题:基础模拟题: 导弹拦截:动态规划.贪心: 三国游戏:贪心.博弈论. 数字统计 题目链接:洛谷 P1179 这道题目是一道基础题. 我们只需要开一个变量 ...
- 2017年NOIP普及组复赛题解
题目涉及算法: 成绩:入门题: 图书管理员:模拟: 棋盘:最短路/广搜: 跳房子:RMQ/二分答案/DP(本人解法). 成绩 题目链接:https://www.luogu.org/problemnew ...
- 2014年NOIP普及组复赛题解
题目涉及算法: 珠心算测验:枚举: 比例简化:枚举: 螺旋矩阵:模拟: 子矩阵:状态压缩/枚举/动态规划 珠心算测验 题目链接:https://www.luogu.org/problem/P2141 ...
- 2013年NOIP普及组复赛题解
题目涉及算法: 计数问题:枚举: 表达式求值:栈: 小朋友的数字:动态规划: 车站分级:最长路. 计数问题 题目链接:https://www.luogu.org/problem/P1980 因为数据量 ...
- 2011年NOIP普及组复赛题解
题目涉及算法: 数字反转:模拟: 统计单词数:模拟: 瑞士轮:模拟/排序: 表达式的值:后缀表达式/DP. 数字反转 题目链接:https://www.luogu.org/problem/P1307 ...
- 2008年NOIP普及组复赛题解
题目涉及算法: ISBN号码:简单字符串模拟: 排座椅:贪心: 传球游戏:动态规划: 立体图:模拟. ISBN号码 题目链接:https://www.luogu.org/problem/P1055 简 ...
- 2005年NOIP普及组复赛题解
题目涉及算法: 陶陶摘苹果:入门题: 校门外的树:简单模拟: 采药:01背包: 循环:模拟.高精度. 陶陶摘苹果 题目链接:https://www.luogu.org/problem/P1046 循环 ...
- 2018年NOIP普及组复赛题解
题目涉及算法: 标题统计:字符串入门题: 龙虎斗:数学题: 摆渡车:动态规划: 对称二叉树:搜索. 标题统计 题目链接:https://www.luogu.org/problem/P5015 这道题目 ...
- 2015年NOIP普及组复赛题解
题目涉及算法: 金币:入门题: 扫雷游戏:入门题: 求和:简单数学推导: 推销员:贪心. 金币 题目链接:https://www.luogu.org/problem/P2669 入门题,直接开一个循环 ...
随机推荐
- 【JZOJ3623】【SDOI2014】数表(table) 树状数组+离线+莫比乌斯反演
题面 100 \[ Ans=\sum_{i=1}^n\sum_{j=1}^mg(gcd(i,j)) \] 其中, \[ g(d)=\sum_{i|d}i \] 我们注意到\(gcd(i,j)\)最多有 ...
- C++学习笔记(1)-构造函数与析构函数
1.C++规定,每个类必须有默认的构造函数,没有构造函数就不能创建对象. 2.若没有提供任何构造函数,那么c++自动提供一个默认的构造函数,该默认构造函数是一个没有参数的构造函数,它仅仅负责创建对象而 ...
- 【风马一族_php】
原文来自:http://www.cnblogs.com/sows/p/6054383.html (博客园的)风马一族 侵犯版本,后果自负 2016-11-11 15:13:51 回顾 数组:分配 ...
- 【python练手】获取城市天气质量
#!/usr/bin/python # -*- coding: utf-8 -*- # get city pm2.5 and ranking # python2.7 import sys import ...
- 对快速排序的分析 Quick Sort
快速排序 快排的基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序.通常可选第一个记录为基准 ...
- nodeJs学习-19 个人博客案例-(1)数据字典
智能社视频27.28 数据字典: 定义: url 300字 admin_table 管理员用户表 ID username varchar(32) password varchar(32) banner ...
- 2019-10-31-C#-强转空会不会出现异常
title author date CreateTime categories C# 强转空会不会出现异常 lindexi 2019-10-31 8:53:6 +0800 2019-9-10 11:4 ...
- 外贸电子商务网站之Prestashop paypal支付添加
1.在https://addons.prestashop.com/en/payment-card-wallet/1748-paypal.html 下载paypal支付模块 2.解压,复制到网站根目录- ...
- SFINAE and enable_if
There's an interesting issue one has to consider when mixing function overloading with templates in ...
- QT,QLabel添加超链接
1.方法1:使用信号槽绑定方式 //设置超链接并绑定信号槽QLabel *linkLabel = new QLabel(); linkLabel->setText("<a hre ...