Asia Hong Kong Regional Contest 2016 Playing with Numbers

原题:

https://open.kattis.com/problems/playingwithnumbers

You have a list of  N N numbers, each of the form  2a3b 2a3b for some non-negative integers  a aand  b b. You want to perform  N1 N−1 operations on these numbers. Each operation acts on two numbers  X X and  Y Y of your choice from the list, replacing them with a new number  op(X,Y) op(X,Y). After each operation, your list has one fewer number.

In this task, an operation  op op can be  gcd gcd or  lcm lcm (they stand for greatest common divisor and least common multiple, respectively). There are a total of  N N scenarios: You may apply “ gcd gcd” operations  k k times and “ lcm lcm” operations  N1k N−1−k times. For each of the  N N scenarios, what is the largest possible outcome after these  N1 N−1operations? What about the smallest possible outcome?

Input

The first line consists of a single integer  N N ( 1N50000 1≤N≤50000). The following  N Nlines each contains a pair of integers  ai ai and  bi bi ( 0ai,bi1000 0≤ai,bi≤1000), indicating that the  i ith number in your initial list is  2ai3bi 2ai3bi.

Output

Output  N N lines in total. On line  i i ( i=1,,N i=1,…,N), output four space-separated integers  a,b,a a,b,a′ and  b b′. The first pair of integers  a a and  b b indicate that the largest possible outcome is  2a3b 2a3b with  i1 i−1 “ gcd gcd” operations (and therefore  Ni N−i “ lcm lcm” operations). The second pair of integers  a a′ and  b b′ indicate that the smallest possible outcome is  2a3b 2a′3b′, again with  i1 i−1 “ gcd gcd” operations.

Explanation for sample data

The three numbers are  2030=1 2030=1 2132=18 2132=18, and  2230=4 2230=4.

  1. When  i=0 i=0, we can only take  lcm lcm lcm(1,18,4)=36=2232 lcm(1,18,4)=36=2232.

  2. When  i=1 i=1, the largest outcome is  lcm(18,gcd(1,4))=18=2132 lcm(18,gcd(1,4))=18=2132, and the smallest outcome is  gcd(1,lcm(18,4))=1=2030 gcd(1,lcm(18,4))=1=2030.

  3. When  i=2 i=2, we can only take  gcd gcd gcd(1,18,4)=1=2030 gcd(1,18,4)=1=2030.

Sample Input 1Sample 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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值