UPC Contest2833 - 2021个人训练赛第17场

问题 A: The Number of Even Pairs

时间限制: 1 Sec  内存限制: 128 MB

题目描述

We have N+M balls, each of which has an integer written on it.
It is known that:
The numbers written on N of the balls are even.
The numbers written on M of the balls are odd.
Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.
It can be shown that this count does not depend on the actual values written on the balls.

Constraints
·0≤N,M≤100
·2≤N+M
·All values in input are integers.

 

输入

Input is given from Standard Input in the following format:

N M

 

输出

Print the answer.

样例输入

【样例1】
2 1
【样例2】
4 3
【样例3】
1 1
【样例4】
13 3
【样例5】
0 3

样例输出

【样例1】
1
【样例2】
9
【样例3】
0
【样例4】
81
【样例5】
3

提示

样例1解释
For example, let us assume that the numbers written on the three balls are 1,2,4.
If we choose the two balls with 1 and 2, the sum is odd;
If we choose the two balls with 1 and 4, the sum is odd;
If we choose the two balls with 2 and 4, the sum is even.
Thus, the answer is 1. 

 

从奇数集合与偶数集合中任选两个数,相加和为偶数。

简单的数学定理:奇数 + 奇数 = 偶数;  偶数 + 偶数 = 偶数

问题转化:奇数 + 偶数 不为偶数,所以不考虑两个数来自两个集合的情况,现在只需求从奇数集取2个数的方案数与从偶数集取两数的方案数之和(排列组合问题)。

假定有n个数,从中取两个(不考虑顺序)下可得方案数公式:n * (n - 1) / 2。

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<functional>
#include<limits.h>
#include<float.h>
#define pi 3.14159265358979323846264338327950288419716939937510582097494
using namespace std;
#define _for(i, a, b) for(long long i=(a);i<(b);++i)
#define _rep(i, a, b) for(long long i=(a);i<=(b);++i)
#define _re_for(i, a, b)	for(long long i=(a);i>(b);--i)
#define _re_rep(i, a, b)	for(long long i=(a);i>=(b);--i)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int INF1 = 0x3f3f3f3f;
const int INF0 = 0xc0c0c0c0;
const int MAX = 10010;
// INT_MAX       INT_MIN
// DBL_MAX       DBL_MIN
// -std=c++17 -Wl,--stack=536870914
ll read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')      f = -1;
		c = getchar();
	}
	while (isdigit(c))   x = x * 10 + (c - 48), c = getchar();
	return x * f;
}

ll sol(ll n) {
	return n * (n - 1) >> 1;
}

int main() {
	ll n = read(), m = read();
	cout << sol(n) + sol(m);
	return 0;
}

 

 

问题 B: String Palindrome

时间限制: 1 Sec  内存限制: 128 MB

题目描述

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:
·S is a palindrome.
·Let N be the length of S. The string formed by the 1-st through ((N−1)/2)-th characters of S is a palindrome.
·The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.
Determine whether S is a strong palindrome.

Constraints
·S consists of lowercase English letters.
·The length of S is an odd number between 3 and 99 (inclusive).

输入

Input is given from Standard Input in the following format:

S

 

输出

If S is a strong palindrome, print Yes; otherwise, print No.

样例输入

【样例1】
akasaka
【样例2】
level
【样例3】
atcoder

样例输出

【样例1】
Yes
【样例2】
No
【样例3】
No

提示

样例1解释
S is akasaka.
The string formed by the 1-st through the 3-rd characters is aka.
The string formed by the 5-th through the 7-th characters is aka. All of these are palindromes, so S is a strong palindrome.

 

三个条件判断回文串:整个串对称、s[1]到s[(n - 1) / 2]对称、s[(n + 3) / 2]到s[n]对称。

使用stl中的翻转函数构造一个与待判字符串相反的字符串,若连个串相等,即两串具有对称性,即回文。

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<functional>
#include<limits.h>
#include<float.h>
#define pi 3.14159265358979323846264338327950288419716939937510582097494
using namespace std;
#define _for(i, a, b) for(long long i=(a);i<(b);++i)
#define _rep(i, a, b) for(long long i=(a);i<=(b);++i)
#define _re_for(i, a, b)	for(long long i=(a);i>(b);--i)
#define _re_rep(i, a, b)	for(long long i=(a);i>=(b);--i)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int INF1 = 0x3f3f3f3f;
const int INF0 = 0xc0c0c0c0;
const int MAX = 10010;
// INT_MAX       INT_MIN
// DBL_MAX       DBL_MIN
// -std=c++17 -Wl,--stack=536870914
ll read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')      f = -1;
		c = getchar();
	}
	while (isdigit(c))   x = x * 10 + (c - 48), c = getchar();
	return x * f;
}

string a, b;
int main() {
	cin >> a;
	b = a;
	reverse(b.begin(), b.end());
	if (a != b) {
		cout << "No";
		return 0;
	}
	string test1;
	_rep(i, 0, ((a.size() - 1) >> 1) - 1) {
		test1 += a[i];
	}
	string temp1 = test1;
	reverse(temp1.begin(), temp1.end());
	if (temp1 != test1) {
		cout << "No";
		return 0;
	}
	string test2;
	_rep(i, ((a.size() + 3) >> 1) - 1, a.size() - 1) {
		test2 += a[i];
	}
	string temp2 = test2;
	reverse(temp2.begin(), temp2.end());
	if (temp2 != test2) {
		cout << "No";
		return 0;
	}
	cout << "Yes";
	return 0;
}

 

 

问题 C: Maximum Volume

时间限制: 1 Sec  内存限制: 128 MB  Special Judge

题目描述

Given is a positive integer L. Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints
·1≤L≤1000
·L is an integer.

输入

Input is given from Standard Input in the following format:

L

输出

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L. Your output is considered correct if its absolute or relative error from our answer is at most 10−6.

样例输入

【样例1】
3
【样例2】
999

样例输出

【样例1】
1.000000000000
【样例2】
36926037.000000000000

提示

样例1解释
For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.
On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

 

给出一立方体的维度之和(长 + 宽 + 高),问在三维之和给定的情况下体积的最大值。

简单的数学知识

周长一定的矩形要使其面积最大,那么这个矩形必然是正方形,推广到三维,便是正立方体。

 

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<functional>
#include<limits.h>
#include<float.h>
#define pi 3.14159265358979323846264338327950288419716939937510582097494
using namespace std;
#define _for(i, a, b) for(long long i=(a);i<(b);++i)
#define _rep(i, a, b) for(long long i=(a);i<=(b);++i)
#define _re_for(i, a, b)	for(long long i=(a);i>(b);--i)
#define _re_rep(i, a, b)	for(long long i=(a);i>=(b);--i)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int INF1 = 0x3f3f3f3f;
const int INF0 = 0xc0c0c0c0;
const int MAX = 10010;
// INT_MAX       INT_MIN
// DBL_MAX       DBL_MIN
// -std=c++17 -Wl,--stack=536870914
ll read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')      f = -1;
		c = getchar();
	}
	while (isdigit(c))   x = x * 10 + (c - 48), c = getchar();
	return x * f;
}

double tol;

int main() {
	cin >> tol;
	cout << fixed << setprecision(12) << pow(tol / 3, 3);// 精度大些
	return 0;
}

 

 

问题 D: Banned K

时间限制: 1 Sec  内存限制: 128 MB

题目描述

We have N balls. The i-th ball has an integer Ai written on it.For each k=1,2,...,N, solve the following problem and print the answer.
Find the number of ways to choose two distinct balls (disregarding order) from the N−1 balls other than the k-th ball so that the integers written on them are equal.

Constraints
·3≤N≤2×105
·1≤Ai≤N
·All values in input are integers.

输入

Input is given from Standard Input in the following format:

N
A1 A2 ... AN

 

输出

For each k=1,2,...,N, print a line containing the answer.

样例输入

【样例1】
5
1 1 2 1 2
【样例2】
4
1 2 3 4
【样例3】
5
3 3 3 3 3
【样例4】
8
1 2 1 4 2 1 4 1

样例输出

【样例1】
2
2
3
2
3
【样例2】
0
0
0
0
【样例3】
6
6
6
6
6
【样例4】
5
7
5
7
7
5
7
5

提示

样例1解释
Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.
From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.
Thus, the answer for k=1 is 2.
样例2解释
No two balls have equal numbers written on them.
样例3解释
Any two balls have equal numbers written on them.

 

从一堆数里从头到尾选出除S[i]外相同数组成的二元组的方案数。

选择方案公式与题A一样,唯一不同的地方在于要做N次的循环,输出除S[i]外的方案数。

若一个数有m个,那么该数便有 m * (m - 1)种方案。

每次循环的结果与所有数(S[i]不排除)的方案数只有含不含S[i]的区别,说人话便是单次循环的结果为:所有数的方案数 - 数i的方案数 + 数i少一个的方案数。

特别的,若一个数个数不足2,那么该数的方案只有0,对答案无影响,便不考虑次数不足2的数。

 

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<functional>
#include<limits.h>
#include<float.h>
#define pi 3.14159265358979323846264338327950288419716939937510582097494
using namespace std;
#define _for(i, a, b) for(long long i=(a);i<(b);++i)
#define _rep(i, a, b) for(long long i=(a);i<=(b);++i)
#define _re_for(i, a, b)	for(long long i=(a);i>(b);--i)
#define _re_rep(i, a, b)	for(long long i=(a);i>=(b);--i)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int INF1 = 0x3f3f3f3f;
const int INF0 = 0xc0c0c0c0;
const int MAX = 10010;
// INT_MAX       INT_MIN
// DBL_MAX       DBL_MIN
// -std=c++17 -Wl,--stack=536870914
ll read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')      f = -1;
		c = getchar();
	}
	while (isdigit(c))   x = x * 10 + (c - 48), c = getchar();
	return x * f;
}

ll n;
unordered_map<ll, ll>mp;
vector<ll>vec;
ll num, sum;

ll sol(ll n) {
	return (n * (n - 1)) >> 1;
}

int main() {
	n = read();
	_rep(i, 1, n) {
		num = read();
		vec.push_back(num);// 记录要判断的对象
		mp[num]++;// 每种数据的个数
	}
	for (auto &it : mp)	if (it.second >= 2)	sum += sol(it.second);// 求总的方案数
	for (auto it : vec)	printf("%lld\n", sum + (mp[it] >= 2 ? -sol(mp[it]) + sol(mp[it] - 1) : 0));
	return 0;
}

 

 

问题 G: 时间

时间限制: 1 Sec  内存限制: 128 MB

题目描述

众所周知,NOIP及其模拟赛的比赛时长均为3小时30分钟。
Alice和Bob参加了于h时m分开始的2020牛客NOIP赛前集训营-提高组,请你告诉他们比赛结束的时间。

输入

共一行:一个形如hh:mm的字符串,表示比赛开始的时间。位数不足将填补0。

输出

共一行:一个形如hh:mm的字符串,表示比赛结束的时间。位数不足请填补0。

样例输入

【样例1】 
00:00 
【样例2】 
00:30 
【样例3】 
20:30 

样例输出

【样例1】 
03:30 
【样例2】 
04:00 
【样例3】 
00:00 

提示

存在独立的30%的测试点,满足h<20,m<30。
存在独立的30%的测试点,满足h<20。
存在独立的30%的测试点,满足m<30。
对于全部的数据,满足0≤h<24,0≤m<60。
注:独立指测试点集合互不相交。

 

语法题,输入和输出注意下,判断小时和分钟是否溢出,溢出则进行模处理。

 

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<functional>
#include<limits.h>
#include<float.h>
#define pi 3.14159265358979323846264338327950288419716939937510582097494
using namespace std;
#define _for(i, a, b) for(long long i=(a);i<(b);++i)
#define _rep(i, a, b) for(long long i=(a);i<=(b);++i)
#define _re_for(i, a, b)	for(long long i=(a);i>(b);--i)
#define _re_rep(i, a, b)	for(long long i=(a);i>=(b);--i)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int INF1 = 0x3f3f3f3f;
const int INF0 = 0xc0c0c0c0;
const int MAX = 10010;
// INT_MAX       INT_MIN
// DBL_MAX       DBL_MIN
// -std=c++17 -Wl,--stack=536870914
ll read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')      f = -1;
		c = getchar();
	}
	while (isdigit(c))   x = x * 10 + (c - 48), c = getchar();
	return x * f;
}

int h, m;

int main() {
	scanf("%2d:%2d", &h,  &m);
	h += 3;
	m += 30;
	if (m >= 60) {
		h++;
		m -= 60;
		if (h >= 24) {
			h %= 24;
		}
	}
	else	h %= 24;
	printf("%02d:%02d", h, m);
	return 0;
}

 

 

问题 H: 石子

时间限制: 1 Sec  内存限制: 128 MB

题目描述

Alice和Bob从小就一起玩石子。
有一天,他们又想愉快的玩一个石子游戏。
一共有n堆石子,第i堆石子有ai个,两人轮流操作。
Alice走先手,每个人每个回合只能对一堆石子进行操作,Alice每次操作只能拿偶数个石子,Bob每次操作只能拿奇数个石子,每次操作至少拿走一个石子,直到一方无法进行任何操作,无法操作的人失败。
假设Alice与Bob都是绝顶聪明的,如果Alice可以获胜,那么输出YES,否则输出NO。

输入

多组数据。对于每组数据,第一行输入一个正整数n,第二行输入n个正整数,第i个数表示ai。

输出

对于每组数据,每行输出一个字符串YES或NO。

样例输入

【样例1】 
2   
2 1 
【样例2】 
1 
6 

样例输出

【样例1】 
NO 
【样例2】 
YES

提示

对于20%的数据,数据组数为1且n=1
对于另外20%的数据,n=1;
对于另外20%的数据,石子数都为1;
对于100%的数据n的和小于1000000,石子数小于1000000000

 

博弈论

Bob可以使偶数堆便奇数堆,使得Alice取数时,使得Bob可以继续取剩下的奇数堆;而Alice取数之后不会改变数堆的奇偶性,这样看Bob是占优势的。

再加下贪心思维:只要能有一个数堆可以剩下1(Ailce无论如何也取不了改堆的数),那么Bob就赢了。

唯一的特例:只有一堆数——是偶数则Alice赢。

 

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<functional>
#include<limits.h>
#include<float.h>
#define pi 3.14159265358979323846264338327950288419716939937510582097494
using namespace std;
#define _for(i, a, b) for(long long i=(a);i<(b);++i)
#define _rep(i, a, b) for(long long i=(a);i<=(b);++i)
#define _re_for(i, a, b)	for(long long i=(a);i>(b);--i)
#define _re_rep(i, a, b)	for(long long i=(a);i>=(b);--i)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int INF1 = 0x3f3f3f3f;
const int INF0 = 0xc0c0c0c0;
const int MAX = 10010;
// INT_MAX       INT_MIN
// DBL_MAX       DBL_MIN
// -std=c++17 -Wl,--stack=536870914
ll read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')      f = -1;
		c = getchar();
	}
	while (isdigit(c))   x = x * 10 + (c - 48), c = getchar();
	return x * f;
}

ll n;

int main() {
	while (cin >> n) {
		ll cnt = 0;
		ll num;
		_rep(i, 1, n) {
			num = read();
			if (!(num % 2))	cnt ++;
		}
		if (cnt == 1 && n == 1)	cout << "YES" << endl;
		else					cout << "NO" << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值