1361: [蓝桥杯2018初赛]乘积尾零
#include <bits/stdc++.h>
using namespace std;
int main() {
int count2 = 0, count5 = 0;
for(int i = 0; i < 10; i ++) {
for(int j = 0; j < 10; j ++) {
int x; cin >> x;
while(x > 0) {
if(x%2 == 0) {
count2 ++, x /= 2;
}
else break;
}
while(x > 0) {
if(x%5 == 0) {
count5 ++, x /= 5;
}
else break;
}
}
}
cout << min(count2, count5) << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << 31 << endl;
return 0;
}
1365: [蓝桥杯2018初赛]全球变暖
#include <bits/stdc++.h>
using namespace std;
string a[1010];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct node {
int x, y;
};
void bfs(int x, int y) {
queue<node> q;
node tmp; tmp.x = x; tmp.y = y;
a[tmp.y][tmp.x] = '%';
q.push(tmp);
while(!q.empty()) {
tmp = q.front(); q.pop();
for(int i = 0; i < 4; i ++) {
node next = tmp;
next.x += dx[i]; next.y += dy[i];
if(a[next.y][next.x] == '#' || a[next.y][next.x] == '!') {
a[next.y][next.x] = '%';
q.push(next);
}
}
}
}
int main() {
int n; cin >> n;
for(int i = 0; i < n; i ++) {
cin >> a[i];
}
int pre_count = 0;
for(int y = 1; y < n - 1; y ++) {
for(int x = 1; x < n - 1; x ++) {
if(a[y][x] == '#') {
pre_count ++;
bfs(x, y);
}
}
}
for(int y = 1; y < n - 1; y ++) {
for(int x = 1; x < n - 1; x ++) {
if(a[y][x] == '%') {
a[y][x] = '#';
}
}
}
for(int y = 1; y < n - 1; y ++) {
for(int x = 1; x < n - 1; x ++) {
if (a[y][x] == '#') {
for (int i = 0; i < 4; i ++) {
int nextx = x + dx[i];
int nexty = y + dy[i];
if (a[nexty][nextx] == '.') {
a[y][x] = '!';
break;
}
}
}
}
}
int post_count = 0;
for(int y = 1; y < n - 1; y ++) {
for(int x = 1; x < n - 1; x ++) {
if (a[y][x] == '#') {
post_count ++;
bfs(x, y);
}
}
}
cout << pre_count - post_count << endl;
return 0;
}
1368: [蓝桥杯2018初赛]第几天
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << 31+29+31+30+4 << endl;
return 0;
}
1369: [蓝桥杯2018初赛]明码
#include <bits/stdc++.h>
using namespace std;
string a, b; int a1, a2;
void f(int m, int n) {
if(m >= 0) {
for(int i = 0; i < 7; i ++) {
if(m >> i&1) a[7-i] = '1';
}
}
else {
a[0] = '1';
for(int i = 0; i < 7; i ++) {
if((m+128) >> i&1) a[7-i] = '1';
}
}
if(n >= 0) {
for(int i = 0; i < 7; i ++) {
if(n >> i&1) b[7-i] = '1';
}
}
else {
b[0] = '1';
for(int i = 0; i < 7; i ++) {
if((n+128) >> i&1) b[7-i] = '1';
}
}
}
int main() {
for(int i = 0; i < 16; i ++) {
for(int j = 0; j < 16; j ++) {
cin >> a1 >> a2;
a = "--------"; b = "--------";
f(a1, a2);
cout << a << b << endl;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int ansm = pow(9, 9);
cout << ansm << endl;
return 0;
}
1370: [蓝桥杯2018初赛]测试次数
#include <bits/stdc++.h>
using namespace std;
int dp[1005][10];
int main() {
int n = 1000, m = 3;
for(int i = 1; i <= n; i ++) {
dp[i][1] = i;
}
for(int j = 2; j <= m; j ++) {
for(int i = 1; i <= n; i++) {
dp[i][j] = 2e9;
for(int k = 2; k <= i; k ++)
dp[i][j] = min(dp[i][j],1 + max(dp[k-1][j-1], dp[i-k][j]));
}
}
cout << dp[n][m] << endl;
return 0;
}
1371: [蓝桥杯2018初赛]递增三元组
#include <bits/stdc++.h>
using namespace std;
long long ans;
int main() {
int n; cin >> n;
int a[n], b[n], c[n];
for(int i = 0; i < n; i ++) cin >> a[i];
for(int i = 0; i < n; i ++) cin >> b[i];
for(int i = 0; i < n; i ++) cin >> c[i];
sort(a, a+n); sort(b, b+n); sort(c, c+n);
int p = 0, q = 0;
for(int i = 0; i < n; i ++) {
while(p<n && a[p] < b[i]) p ++;
while(q<n && c[q] <= b[i]) q ++;
ans += ((long long)p*(n-q));
}
cout << ans << endl;
return 0;
}
1372: [蓝桥杯2018初赛]螺旋折线
对于整点 (X, Y),我们定义它到原点的距离 dis(X, Y) 是从原点到 (X, Y) 的螺旋折线段的长度。最关键是求出四个表达式即可,还有 dis(-n, -n) 公式的推导。
x = n: dis = 2n + y + n;
x = -n: dis = 6n + n - y;
y = n: dis = 4*n + n - x;
y = -n: dis = n + x;
找出规律之后,通过不同的象限位置,然后解决本题。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
int x, y; cin >> x >> y;
if(abs(x) <= y) {
int n = y;
cout << (LL)(2 * n - 1) * (2 * n) + x - (-n) << endl;
}
else if(abs(y) <= x) {
int n = x;
cout << (LL)(2 * n) * (2 * n) + n - y << endl;
}
else if (abs(x) <= abs(y) + 1 && y < 0) {
int n = abs(y);
cout << (LL)(2 * n) * (2 * n + 1) + n - x << endl;
}
else {
int n = abs(x);
cout << (LL)(2 * n - 1) * (2 * n - 1) + y - (-n + 1) << endl;
}
return 0;
}
1373: [蓝桥杯2018初赛]日志统计
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 100010;
PII logs[N];
int st[N], cnt[N];
int main() {
int n, d, k; cin >> n >> d >> k;
for(int i = 0; i < n; i ++) {
cin >> logs[i].x >> logs[i].y;
}
sort(logs, logs + n);
for(int i = 0, j = 0; i < n; i ++) {
int t = logs[i].y; cnt[t] ++;
while (logs[i].x - logs[j].x >= d) {
cnt[logs[j].y] --; j ++;
}
if(cnt[t] >= k) st[t] = 1;
}
for(int i = 0; i <= 100000; i ++) {
if(st[i]) cout << i << endl;
}
return 0;
}
1374: [蓝桥杯2018初赛]乘积最大
分析题目可以知道要求出乘积的最大值,那么可以先对序列排序,因为序列中有正有负,那么排序后的序列最左边就是最小的负数(如果有负数),也就是绝对值最大的负数,最右边是最大的正数(如果有正数)
并且由于所选择的数的数量,不能确定奇数偶数,所以需要分情况讨论
k = = n:那么答案就是所有值的乘积
k < n 且 k 为偶数:乘积最大值必定为正数。如果序列中负数的个数为偶数,那么总能找到成对的负数,使得其答案为正数。如果序列中负数的个数为奇数,那么我们可以只选择偶数个绝对值最大的负数
k < n 且 k 为奇数:
如果序列中全为负数,那么结果必为负数,为了使得答案最大,我们需要选绝对值最小的数。如果序列中至少存在一个正数,那么我们可以先选择最大的那个正数,k 减少一个,转换为了 k 为偶数的情况。因为如果 k 为奇数,可以通过先选一个特判来转换为 k 为偶数的做法,所以可以同一对 k 为偶数进行操作。
对于本题目,可以采用双指针的算法,两个为一对的移动指针来计算每一对的值,因为序列最左边如果有负数就是绝对值最大的负数,序列最右边如果有正数就是绝对值最大的正数,所以两个指针分别头和尾来一对一对移动,判断哪一对的值最大。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010, mod = 1000000009;
int a[N];
int main() {
int n, k; cin >> n >> k;
for(int i = 0;i < n; i ++) cin >> a[i];
sort(a, a + n);
int res = 1, l = 0, r = n - 1;
int sign = 1;
if(k & 1) {
res = a[r --];
if(res < 0) sign = -1;
k --;
}
while(k) {
LL x = (LL)a[l] * a[l + 1], y = (LL)a[r - 1] * a[r];
if(x * sign > y * sign) {
res = x % mod * res % mod;
l += 2;
}
else {
res = y % mod * res % mod;
r -= 2;
}
k -= 2;
}
cout << res << endl;
return 0;
}