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:
- Line up the numbers vertically matching digits places.
- Add together the numbers that share the same place value from right to left.
- Carry if necessary.
it means when adding two numbers we will get something like this:
![](https://i-blog.csdnimg.cn/blog_migrate/96d6ffb769e0753e0f7188e0027bc8dd.png)
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:
![](https://i-blog.csdnimg.cn/blog_migrate/fec756fb7f4ec82b5b81d7f0e9a74294.png)
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.
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 the smallest and the largest number you can get from adding two numbers from the list using Gaby's addition algorithm.
6 17 5 11 0 42 99
0 99
7 506823119072235413 991096248449924896 204242310783332529 778958050378192979 384042493592684633 942496553147499866 410043616343857825
52990443860776502 972190360051424498
In the first sample input this is how you get the minimum and the maximum value
![](https://i-blog.csdnimg.cn/blog_migrate/e08021af965b91e2dd6bc56e35703ad3.png)
用字典树存储所有的数字,再一个一个贪心的去匹配就好了。
注意不能取相同编号的两个数。
#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;
}
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:
![](https://i-blog.csdnimg.cn/blog_migrate/a49f72b1ce451544bb095b5fdd264f9e.png)
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.
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).
Print one number, the maximum number of nodes Ivan can draw. It is guaranteed that the answer fits on a 63-bits integer.
3 2 1 3
16
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;
}