原题:
https://open.kattis.com/problems/playingwithnumbers
You have a list of N numbers, each of the form 2a3b for some non-negative integers a and b . You want to perform N−1 operations on these numbers. Each operation acts on two numbers X and Y of your choice from the list, replacing them with a new number op(X,Y) . After each operation, your list has one fewer number.
In this task, an operation op can be gcd or lcm (they stand for greatest common divisor and least common multiple, respectively). There are a total of N scenarios: You may apply “ gcd ” operations k times and “ lcm ” operations N−1−k times. For each of the N scenarios, what is the largest possible outcome after these N−1 operations? What about the smallest possible outcome?
Input
The first line consists of a single integer N ( 1≤N≤50000 ). The following N lines each contains a pair of integers ai and bi ( 0≤ai,bi≤1000 ), indicating that the i th number in your initial list is 2ai3bi .
Output
Output N lines in total. On line i ( i=1,…,N ), output four space-separated integers a,b,a′ and b′ . The first pair of integers a and b indicate that the largest possible outcome is 2a3b with i−1 “ gcd ” operations (and therefore N−i “ lcm ” operations). The second pair of integers a′ and b′ indicate that the smallest possible outcome is 2a′3b′ , again with i−1 “ gcd ” operations.
Explanation for sample data
The three numbers are 2030=1 , 2132=18 , and 2230=4 .
-
When i=0 , we can only take lcm . lcm(1,18,4)=36=2232 .
-
When i=1 , the largest outcome is lcm(18,gcd(1,4))=18=2132 , and the smallest outcome is gcd(1,lcm(18,4))=1=2030 .
-
When i=2 , we can only take gcd . gcd(1,18,4)=1=2030 .
Sample Input 1 | Sample Output 1 |
---|---|
3 0 0 1 2 2 0 | 2 2 2 2 1 2 0 0 0 0 0 0 |
取对数排序,只需要考虑第一第二和倒数第一第二的输出
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <deque>
#include <string>
#include <cmath>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <sstream>
#include <climits>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define pi acos(-1.0)
#define INF 2147483647
using namespace std;
typedef long long ll;
typedef pair <int,int > P;
int N,i,j;
struct Number
{
int a,b;
double m;
} number[50005];
bool cmp(const Number&q,const Number&w)
{
return q.m<w.m;
}
int main()
{
scanf("%d",&N);
memset(number,0,sizeof(number));
int a_min=INF,b_min=INF,a_max=-1,b_max=-1;
for(i=0; i<N; i++)
{
scanf("%d%d",&number[i].a,&number[i].b);
a_min=min(a_min,number[i].a);
a_max=max(a_max,number[i].a);
b_min=min(b_min,number[i].b);
b_max=max(b_max,number[i].b);
double flag=log2(3);
number[i].m=flag*number[i].b+number[i].a;
}
sort(number,number+N,cmp);
printf("%d %d %d %d\n",a_max,b_max,a_max,b_max);
for(i=1; i<N-1; i++)
{
if(i==N-2)
{
printf("%d %d %d %d\n",number[N-1].a,number[N-1].b,a_min,b_min);
}
else if(i==1)
{
printf("%d %d %d %d\n",a_max,b_max,number[0].a,number[0].b);
}
else
printf("%d %d %d %d\n",a_max,b_max,a_min,b_min);
}
if(N>1)
printf("%d %d %d %d\n",a_min,b_min,a_min,b_min);
return 0;
}