2018 Multi-University Training Contest 1 (ing)

题目链接

 1001. Maximum Multiple

 

Maximum Multiple

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 531    Accepted Submission(s): 237


 

Problem Description

Given an integer n, Chiaki would like to find three positive integers x, y and z such that: n=x+y+z, x∣n, y∣n, z∣n and xyz is maximum.

 

 

Input

There are multiple test cases. The first line of input contains an integer T (1≤T≤106), indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106).

 

 

Output

For each test case, output an integer denoting the maximum xyz. If there no such integers, output −1 instead.

 

 

Sample Input

3 1 2 3

 

 

Sample Output

-1 -1 1 

题目描述:

T组数据,给出一个n,找出一组x, y, z,使得 n = x + y + z,且x * y * z 的乘积最大,输出最大乘积。

Solution:

由于对称性,不妨设 2 <= x <= y <= z。

第一种证明思路:由于2 <= x <= y <= z,所以xy ≤ yz,xz ≤ yz,对于式子1/x + 1/y + 1/z = 1两边都乘以xyz,可得出 yz+xz+xy=xyz,所以 xyz = yz + xz + xy ≤ yz + yz + yz = 3yz,不难得出 x ≤ 3。

当x=2时,1/y + 1/z = 1/2,2(z+y) = yz,(y-2)(z-2) = 4,所以y-2=1,z-2=4;或y-2=2,z-2=2;或y-2=4,z-2=1。所以可以得出两组解为(2, 4, 4)和(3,3,6)

当x=3时,1/y + 1/z = 2/3,3(z+y) = 2yz,于是(2y-3)(2z-3) = 9,所以2y-3=1,2z-3=9;或2y-3=3,2z-3=3;或2y-3=9,2z-3=1。于是得到解(3,3,3)

第二种证明思路:首先 1/3 + 1/3 + 1/3 = 1,所以,对于每个x,y,z 都不可能同时大于3,所以 x的取值只能是2、3。

当x=2时,1/y + 1/z = 1/2,仅有 y = z = 4 和 y = 3,z = 6 ( 当 z > 6 时,1/z = 1/2 - 1/y < 1/6,即 y < 3,穷举y无解)
当x=3时,y = z = 3 (若y > 3, 则 z > 3,无解)

综上以上两种思路:

可以得到三组解为(3,3,3)、(2,4,4)、(3,3,6),显然第三组解可以舍掉,因为能整除6的一定也能整除3,明显第一组解更优于第三组解,欲使x * y * z最大,即是当x、y、z的值最接近的时候,对前两组解特判一下就好了。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <stack>
#define mst(a, b) memset(a, b, sizeof(a))
#define rush() int T; cin >> T; while(T--)
using namespace std;
typedef long long LL;
const int MaxN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const LL Mod = 998244353;
const double eps = 1e-9;

int main() 
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	rush() {
		LL n; cin >> n;
		if(n % 3 == 0) cout << ((n * n * n) / 27LL) << endl;
		else if(n % 4 == 0) cout << ((n * n * n) / 32LL) << endl;
		else cout << -1 << endl;
	}
	return 0;
}

 

1002. Balanced Sequence

Balanced Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1065    Accepted Submission(s): 246

Problem Description

Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type is said to be balanced:

+ if it is the empty string
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced.

Chiaki can reorder the strings and then concatenate them get a new string t. Let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. Chiaki would like to know the maximum value of f(t) for all possible t.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤105) -- the number of strings.
Each of the next n lines contains a string si (1≤|si|≤105) consisting of `(' and `)'.
It is guaranteed that the sum of all |si| does not exceeds 5×106.

Output

For each test case, output an integer denoting the answer.

Sample Input

2 1 )()(()( 2 ) )(

Sample Output

4 2

题目描述:

T组数据,给出n个只由 '(' 和 ')' 组成的字符串,每个字符串长度不超过1e5,保证n个串的总长度不超过5e6。通过对n个字符串的任意组合,求最长的balanced subsequence(可以不连续)的长度。balanced subsequence 的规定如下:一个"()"  的长度为2。

Solution:

代码:

1003. Triangle Partition

Problem Description

Chiaki has 3n points p1,p2,…,p3n. It is guaranteed that no three points are collinear.
Chiaki would like to construct n disjoint triangles where each vertex comes from the 3n points.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤1000) -- the number of triangle to construct.
Each of the next 3n lines contains two integers xi and yi (−109≤xi,yi≤109).
It is guaranteed that the sum of all n does not exceed 10000.

Output

For each test case, output n lines contain three integers ai,bi,ci (1≤ai,bi,ci≤3n) each denoting the indices of points the i-th triangle use. If there are multiple solutions, you can output any of them.

Sample Input

1 1 1 2 2 3 3 5

Sample Output

1 2 3

题目描述:

T组数据,给出 3*n 个点,通过这些点组成n个不相交的三角形,输出每个三角形使用的点的编号。

Solution:

题目保证不会有三点共线,所以只需要对 3*n个点的横坐标排个序,依次取三个点组成一个三角形即可。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <stack>
#define mst(a, b) memset(a, b, sizeof(a))
#define _rush() int T; cin >> T; while(T--)
#define rush() int T; scanf("%d", &T); while(T--)
using namespace std;
typedef long long LL;
const int MaxN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const LL Mod = 998244353;
const double eps = 1e-9;

struct node {
	LL x, y;
	int id;
}a[3005];

int n;

bool cmp(node a, node b) { return a.x < b.x; }

int main() 
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	_rush() {
		cin >> n;
		for(int i = 1; i <= 3 * n; i++) {
			cin >> a[i].x >> a[i].y;
			a[i].id = i;
		}
		sort(a + 1, a + 1 + 3 * n, cmp);
		for(int i = 1; i <= 3 * n; i += 3)
			cout << a[i].id << " " << a[i+1].id << " " << a[i+2].id << endl;
	}
	return 0;
}

1004. Distinct Values

猜你喜欢

转载自blog.csdn.net/Jasmineaha/article/details/81176680