Codeforces Gym 101466 - 2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest

A. Gaby And Addition
time limit per test
6.0 s
memory limit per test
1024 MB
input
standard input
output
standard output

Gaby is a little baby who loves playing with numbers. Recently she has learned how to add 2 numbers using the standard addition algorithm which we summarize in 3 steps:

  1. Line up the numbers vertically matching digits places.
  2. Add together the numbers that share the same place value from right to left.
  3. Carry if necessary.

it means when adding two numbers we will get something like this:

Unfortunately as Gaby is too young she doesn't know what the third step means so she just omitted this step using her own standard algorithm (Gaby's addition algorithm). When adding two numbers without carrying when necessary she gets something like the following:

Gaby loves playing with numbers so she wants to practice the algorithm she has just learned (in the way she learned it) with a list of numbers adding every possible pair looking for the pair which generates the largest value and the smallest one.

She needs to check if she is doing it correctly so she asks for your help to find the largest and the smallest value generated from the list of numbers using Gaby's addition algorithm.

Input

The input starts with an integer n (2 ≤ n ≤ 106) indicating the number of integers Gaby will be playing with. The next line contains n numbers ni (0 ≤ ni ≤ 1018) separated by a single space.

Output

Output the smallest and the largest number you can get from adding two numbers from the list using Gaby's addition algorithm.

Examples
input
6
17 5 11 0 42 99
output
0 99
input
7
506823119072235413 991096248449924896 204242310783332529 778958050378192979 384042493592684633 942496553147499866 410043616343857825
output
52990443860776502 972190360051424498
Note

In the first sample input this is how you get the minimum and the maximum value


用字典树存储所有的数字,再一个一个贪心的去匹配就好了。

注意不能取相同编号的两个数。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#include <sstream>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=10000005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
ll a[maxn],p[55];
int num=1;

struct Tree{   
    int son[10];   
    void init() {  
        for (int i=0;i<10;i++) son[i]=-1;  
    }
};  
Tree tree[20000005];   
  
void insert(int now,int n,ll val) {  
    if (n==-1) return;  
    ll pos=(val/p[n])%10;
    if (tree[now].son[pos]==-1) {  
        tree[now].son[pos]=num++; 
        tree[tree[now].son[pos]].init();
    }
    insert(tree[now].son[pos],n-1,val);  
}  
  
ll findmax(int now,int n,ll val) {
	if (n==-1) return 0;
	ll pos=(val/p[n])%10,ans=0;
	for (int i=9-pos;i>=0;i--)
		if (tree[now].son[i]!=-1) 
				return ((i+pos)%10)*p[n]+findmax(tree[now].son[i],n-1,val);
	for (int i=9;i>9-pos;i--)
		if (tree[now].son[i]!=-1) 
				return ((i+pos)%10)*p[n]+findmax(tree[now].son[i],n-1,val);
}

ll findmin(int now,int n,ll val) {
	if (n==-1) return 0;
	ll pos=(val/p[n])%10,ans=0;
	for (int i=10-pos;i<=9;i++)
		if (tree[now].son[i]!=-1) 
				return ((i+pos)%10)*p[n]+findmin(tree[now].son[i],n-1,val);
	for (int i=0;i<10-pos;i++)
		if (tree[now].son[i]!=-1) 
				return ((i+pos)%10)*p[n]+findmin(tree[now].son[i],n-1,val);
}

int main() {
/*	int t=2;string s;stringstream ss;
		s="";
		ss.clear();
		ss << t;ss >> s; 
		s="input"+s+".in";
		const char* pp = s.data();
		freopen(pp,"r",stdin);
		s="";
		ss.clear();
		ss << t;ss >> s; 
		s="output"+s+".out";
		const char* q = s.data();
		freopen(q,"w",stdout);
		*/
	int n,i,j;
	scanf("%d",&n);
	p[0]=1;
	for (i=1;i<=18;i++) p[i]=p[i-1]*10;
	tree[0].init();num=1;
	ll mn,mx;mn=llinf;mx=-llinf;
	for (i=1;i<=n;i++) {
		scanf("%I64d",&a[i]);
		if (i!=1) {
			mn=min(mn,findmin(0,18,a[i]));
			if (mn==3144) {
				printf("%d\n",i);
			}
			mx=max(mx,findmax(0,18,a[i]));
		}
		insert(0,18,a[i]);
	}
	printf("%I64d %I64d",mn,mx);
	return 0;
}

B. Maximum Tree
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Ivan is a new professor of the University of Nice Algebra Lovers (UNAL), he is going to give a data structures course, that's why he is preparing his slides and wants to draw a beautiful rooted tree. As he loves math, he has a very special array of numbers A and he wants to use each element of the array as the number of children for some level in the tree. For example, if A = [2, 1, 3] two of the possible trees he could draw are the following:

Ivan wants to draw a rooted tree that has the maximum number of nodes. As he is busy with LaTeX he wants you to write a program that computes such number.

Input

The first line consists of an integer n (1 ≤ n ≤ 32), the next line contains n numbers separated by a single space, the elements of A (1 ≤ Ai ≤ 10).

Output

Print one number, the maximum number of nodes Ivan can draw. It is guaranteed that the answer fits on a 63-bits integer.

Example
input
3
2 1 3
output
16
Note

The number of levels of the tree (except for the root) is the same as the number of elements in the array


水题,排序之后累乘即可。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
const int maxn=55,inf=0x3f3f3f3f;  
const ull llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
ull a[maxn];

int main() {
	int n,i;
	ull t,ans;
	t=ans=1;
	cin >> n;
	for (i=1;i<=n;i++) cin >> a[i];
	sort(a+1,a+n+1);
	for (i=n;i>=1;i--) {
		t*=a[i];
		ans+=t;
	}
	cout << ans;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值