Codeforces Round #341 (Div. 2)D. Rat Kwesh and Cheese

D. Rat Kwesh and Cheese
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Wet Shark asked Rat Kwesh to generate three positive real numbers x, y and z, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point.


Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to earn a lot of cheese. He will hand the three numbers x, y and z to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options:


a1 = xyz;
a2 = xzy;
a3 = (xy)z;
a4 = (xz)y;
a5 = yxz;
a6 = yzx;
a7 = (yx)z;
a8 = (yz)x;
a9 = zxy;
a10 = zyx;
a11 = (zx)y;
a12 = (zy)x.
Let m be the maximum of all the ai, and c be the smallest index (from 1 to 12) such that ac = m. Rat's goal is to find that c, and he asks you to help him. Rat Kwesh wants to see how much cheese he gets, so he you will have to print the expression corresponding to that ac.


Input
The only line of the input contains three space-separated real numbers x, y and z (0.1 ≤ x, y, z ≤ 200.0). Each of x, y and z is given with exactly one digit after the decimal point.


Output
Find the maximum value of expression among xyz, xzy, (xy)z, (xz)y, yxz, yzx, (yx)z, (yz)x, zxy, zyx, (zx)y, (zy)x and print the corresponding expression. If there are many maximums, print the one that comes first in the list.


xyz should be outputted as x^y^z (without brackets), and (xy)z should be outputted as (x^y)^z (quotes for clarity).


Sample test(s)
input
1.1 3.4 2.5
output
z^y^x
input
2.0 2.0 2.0
output
x^y^z
input
1.9 1.8 1.7
output

(x^y)^z


题意:给出x,y,z,输出他们之间用上述12方式表达的最大值的格式

分析:

方法一:,对所有式子同时去log,因为一个logx求导之后是1/x(x>0),不改变原来的大小关系,所以我们只需要将其都转化为long double的形式,该题便是可以得到轻松解决

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
long double ans[13];

char s[13][13]={
    " ",
    "x^y^z",
    "x^z^y",
    "(x^y)^z",
    "(x^z)^y",
    "y^x^z",
    "y^z^x",
    "(y^x)^z",
    "(y^z)^x",
    "z^x^y",
    "z^y^x",
    "(z^x)^y",
    "(z^y)^x"
};

int main(){
    long double x,y,z;
    double a,b,c;
    scanf("%lf%lf%lf",&a,&b,&c);
    x=a,y=b,z=c;
    long double xx=log(x);
    long double yy=log(y);
    long double zz=log(z);
    ans[1]=xx*pow(y,z);
    ans[2]=xx*pow(z,y);
    ans[3]=xx*y*z;
    ans[4]=xx*y*z;
    ans[5]=yy*pow(x,z);
    ans[6]=yy*pow(z,x);
    ans[7]=yy*x*z;
    ans[8]=yy*x*z;
    ans[9]=zz*pow(x,y);
    ans[10]=zz*pow(y,x);
    ans[11]=zz*x*y;
    ans[12]=zz*y*x;
    long double tmp=-1;
    int id=0;
    for(int i=1;i<=12;i++)
        if(tmp<ans[i]){
            tmp=ans[i];
            id=i;
        }
    printf("%s",s[id]);
    return 0;
}

方法二:当我们取了一个log的时候,我们发现最大还会出现200^200,这个数值还是很大?

我们再取一个log?分析后我们可以发现如果x原来小于1,取两个log之后会变成负无穷,会影响最后的答案

所以,接下来的情况可以分为两种

1.x,y,z都小于1

2.x,y,z中至少有一个大于等于1

先考虑情况1,如果我们将取两个答案之后的值取倒数,比如

,我们只需要比较他的分母的最小值是多少便可以了

接下来考虑情况2,假设x<1,max(y,z)>=1,我们可以发现"x^y^z", "x^z^y","(x^y)^z", "(x^z)^y",答案均小于1,所以不可能是这四种情况

以此类推

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
double ans[13];
int id[13];
bool flag[13];

char s[13][13]={
    " ",
    "x^y^z",
    "x^z^y",
    "(x^y)^z",
    "(x^z)^y",
    "y^x^z",
    "y^z^x",
    "(y^x)^z",
    "(y^z)^x",
    "z^x^y",
    "z^y^x",
    "(z^x)^y",
    "(z^y)^x"
};

bool cmp(int i,int j){
    return ans[i]<ans[j];
}

bool cmp1(int i,int j){
    if(flag[i]!=flag[j])
        return flag[i]>flag[j];
    return ans[i]>ans[j];
}

int main(){
    double x,y,z,xx,yy,zz;
    scanf("%lf%lf%lf",&x,&y,&z);
    for(int i=1;i<=12;i++){
        id[i]=i;
        flag[i]=true;
    }
    if(x<1&&y<1&&z<1){
        xx=1.0/x;
        yy=1.0/y;
        zz=1.0/z;
        ans[1]=z*log(y)+log(log(xx));
        ans[2]=y*log(z)+log(log(xx));
        ans[3]=log(y*z*log(xx));
        ans[4]=log(y*z*log(xx));
        ans[5]=z*log(x)+log(log(yy));
        ans[6]=x*log(z)+log(log(yy));
        ans[7]=log(x*z*log(yy));
        ans[8]=log(x*z*log(yy));
        ans[9]=y*log(x)+log(log(zz));
        ans[10]=x*log(y)+log(log(zz));
        ans[11]=log(x*y*log(zz));
        ans[12]=log(x*y*log(zz));
        sort(id+1,id+13,cmp);
        printf("%s",s[id[1]]);
    }
    else{
        if(x>=1){
            ans[1]=z*log(y)+log(log(x));
            ans[2]=y*log(z)+log(log(x));
            ans[3]=log(y*z*log(x));
            ans[4]=log(y*z*log(x));
        }
        else
            flag[1]=flag[2]=flag[3]=flag[4]=false;
        if(y>=1){
            ans[5]=z*log(x)+log(log(y));
            ans[6]=x*log(z)+log(log(y));
            ans[7]=log(x*z*log(y));
            ans[8]=log(x*z*log(y));
        }
        else
            flag[5]=flag[6]=flag[7]=flag[8]=false;
        if(z>=1){
            ans[9]=y*log(x)+log(log(z));
            ans[10]=x*log(y)+log(log(z));
            ans[11]=log(x*y*log(z));
            ans[12]=log(x*y*log(z));
        }
        else
            flag[9]=flag[10]=flag[11]=flag[12]=false;
        sort(id+1,id+13,cmp1);
        printf("%s",s[id[1]]);
    }
    return 0;
}


你可能感兴趣的:(Codeforces Round #341 (Div. 2)D. Rat Kwesh and Cheese)