【ATcoder】AtCoder Beginner Contest 159题解

官方题解

落谷链接

ATC链接

A - The Number of Even Pairs

题意

给你两个数$n, m$代表有$n$个偶数,$m$个奇数。让你输出$n$个偶数$m$个奇数从中任选两个数(没有顺序)相加结果为偶数的个数。

题解

相加为偶数只有偶加偶和奇加奇两种情况,其实就是在$n$个数中取两个($C\binom{2}{n}$),在$m$个数中取两个($C\binom{2}{m}$)。

时间复杂度$O(1)$

1 #include <iostream>
2 using namespace std;
3 int main() {
4     long long n, m;
5     cin >> n >> m;
6     cout << n * (n - 1) / 2 + m * (m - 1) / 2;
7     return 0;
8 }

A - The Number of Even Pairs

B - String Palindrome

题意

定义一个字符串 $S$ 是强回文串当且仅当 $S$,$S_{1...\frac{(|s|+1)}{2}}$ 和 $S_{|s|-\frac{(|s|+3)}{2}+1...|s|}$都是回文的。判断一个回文串是不是强回文串。$3 \leq |s| \leq 99$且$|s|$是奇数。

题解

直接取出每一部分的字符串,判断到中心距离相等的位置的字符是否相等即可。

时间复杂度$O(|s|)$。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 char s[100], tmp[100];
 6 int top;
 7 int n, nn;
 8 int main() {
 9     scanf("%s", s + 1);
10     n = strlen(s + 1);
11     nn = n;
12     for (int i = 1; i <= nn; i++) {
13         if (s[i] != s[nn - i + 1]) {
14             puts("No");
15             return 0;
16         }
17     }
18     nn = (n - 1) / 2;
19     for (int i = 1; i <= nn; i++) {
20         if (s[i] != s[nn - i + 1]) {
21             puts("No");
22             return 0;
23         }
24     }
25     nn = (n + 3) / 2;
26     for (int i = nn; i <= n; i++) {
27         tmp[++top] = s[i];
28     }
29     for (int i = 1; i <= top; i++) {
30         if (tmp[i] != tmp[top - i + 1]) {
31             puts("No");
32             return 0;
33         }
34     }
35     puts("Yes");
36     return 0;
37 }

B - String Palindrome

C - Maximum Volume

题意

给定一个整数x。求所有各棱长为实数且和为x的长方体中最大的体积是多少。

题解

小学奥数题...

和一定差小积大。

其实就是一个均值不等式

设棱长为$a,b,c$,则 $a \times b \times c \leq \frac{(a+b+c)^3}{27}=\frac{x^3}{27}$,当$a=b=c$时成立。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 double l;
 5 int main() {
 6     scanf("%lf", &l);
 7     l = l / 3;
 8     printf("%.12lf", l * l * l);
 9     return 0;
10 }

C - Maximum Volume

D - Banned K

题意

我们有n个数,每个数都在[1,n]中,如果去掉第k个数,问剩下的数有多少对相同的数(不计顺序),$1 \leq n \leq 2 \times 10^5$。

题解

我们可以把n个数中有多少相同的数对,因为每个数都很小所以这点我们可以用堆来做。

设cnt[i]代表i这个数出现的次数所以答案等于$\sum_{i=1}^{n}\frac{cnt[i] \times (cnt[i]-1)}{2}$。

现在我们来看删掉一个值为x的数。

cnt[x]的值减小了一,它的贡献变成了$\frac{(cnt[x]-1) \times (cnt[x]-2)}{2}$,相比之前减少了cnt[x]-1。

设不删时的答案是tot,则删掉一个权值为x的数的答案为tot-(cnt[x]-1)。

单词询问复杂度O(1)。

 1 //这里sum就是cnt
 2 #include <iostream>
 3 #include <cstdio>
 4 using namespace std;
 5 const int N = 2e5 + 10;
 6 int n, a[N];
 7 long long sum[N], tot;
 8 int main() {
 9     scanf("%d", &n);
10     for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum[a[i]]++;
11     for (int i = 1; i <= n; i++) {
12         tot += sum[i] * (sum[i] - 1) / 2;
13     }
14     for (int i = 1; i <= n; i++) {
15         printf("%lld\n", tot - (sum[a[i]] - 1));
16     }
17     return 0;
18 }

D - Banned K

E - Dividing Chocolate

我太弱了,我比赛时这题竟然都没调出来。

题意

有一个$n \times m$的矩阵,每个位置要不是要不不是零,用尽量少的次数把这个矩阵切成几块(只能把整行或整列与下一行或列切开,具体可看下面的例子)使得每一块中1的个数少于某个常数。

$1 \leq n \leq 10, 1 \leq m \leq 1000$

题解

我们发现n非常的小,于是我们可以枚举行之间且的情况,再判断列之间要切的情况,取最小值即可。

时间复杂度$O(2^n \times n \times m)$

原文地址:https://www.cnblogs.com/zcr-blog/p/12550421.html

时间: 2024-08-30 02:45:53

【ATcoder】AtCoder Beginner Contest 159题解的相关文章

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 115 题解

题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit : 1024MB Score : 100 points Problem Statement In some other world, today is December D-th. Write a program that prints Christmas if D=25, Christmas E

AtCoder Beginner Contest 121 题解

题目链接:https://atcoder.jp/contests/abc121 A White Cells 分析:题目数据规模很小,直接暴力修改都可以.或者可以推出公式. 代码: 1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int a[25][25] = {0}; 9 int H, W, h, w; 10 scanf("%d %d"

AtCoder Beginner Contest 159

传送门 A - The Number of Even Pairs #include <bits/stdc++.h> #define ll long long using namespace std; int main() { //freopen("in.txt","r",stdin); int n,m; scanf("%d%d",&n,&m); printf("%d\n",n*(n-1)/2+m*(

AtCoder Beginner Contest 144 题解

传送门 $cf$ 自闭了,打 $abc$ 散散心 A - 9x9 ...这个有什么好讲的吗,题目看懂就会做了 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef long long ll; inline int read() { int x=0,f=1; char

AtCoder Beginner Contest 159 E - Dividing Chocolate【枚举】

题目链接 数据范围: \(1≤H≤10\) \(1≤W≤1000\) \(1≤K≤H×W\) 分析: 先观察数据,发现行数特别小,那么我们就可以枚举行的分法,对于每一种分法,求出列的划分数,取最小. 先用二维前缀和,预处理整个图. 复杂度:\(O(2^H*H*W)\) 代码: #include <bits/stdc++.h> using namespace std; const int N=1010; char s[15][N]; int num[15][N]; int main() { in

AtCoder Beginner Contest 160题解

A. 签到题1. #include<bits/stdc++.h> #define fi first #define sd second #define lson (nd<<1) #define rson (nd+nd+1) #define PB push_back #define mid (l+r>>1) #define MP make_pair #define SZ(x) (int)x.size() using namespace std; typedef long

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo