URAL 1971 Graphics Settings 取对数(高精度易超时)

题目大意:

现在给出一个游戏中的n个设置 n <= 10^5; 每个设置对应一个k值, k <= 100

初始的时候的分辨率为w*h

CPU的cycles为p

接下来是m次操作分别代表更改的设置

On 表示打开设置XX, Off是关闭设置XX, Revolution w,h 为更改分辨率至w*h

当前的开启的所有设置对应的k值的乘积乘上分辨率w*h得到一个数A

当 p / A < 10时输出Slideshow

p / A >= 60时输出Perfect

其他情况输出So-so

初始时所有设置都是开着的


大致思路:

刚开始的时候直接就上了java的高精度就TLE了....真是没考虑就直接上了..

后来改用了求对数的方法,虽然存在精度误差但是还是能过的

java 高精度TLE代码 ( 祭奠我冲动的TLE =_= )

Result   :  Time Limit Exceeded

import java.math.BigInteger;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
public class Main{
    public static void main(String args[]){
        int n;
        BigInteger det = BigInteger.valueOf(1);
        Scanner cin = new Scanner(System.in);
        Map<String,Integer> map = new HashMap<String,Integer>();
        n = cin.nextInt();
        BigInteger p,x,y;
        String tmp;
        int t;
        for(int i = 1; i <= n; i++)
        {
            tmp = cin.next();
            t = cin.nextInt();
            map.put(tmp, t);
            det = det.multiply(BigInteger.valueOf(t));
        }
        x = cin.nextBigInteger();
        y = cin.nextBigInteger();
        p = cin.nextBigInteger();
       // System.out.println(x + " " + y + " " + p);
        if(p.compareTo(x.multiply(y).multiply(det).multiply(BigInteger.TEN)) == -1)
        {
           System.out.println("Slideshow");
        }
        else
        {
           if( p.compareTo(x.multiply(y).multiply(det).multiply(BigInteger.valueOf(60))) == -1)
           {
               System.out.println("So-so");
           }
           else
           {
               System.out.println("Perfect");
           }
        }
        int m;
        m = cin.nextInt();
        String w;
        for(int i = 1; i <= m; i++)
        {
            w = cin.next();
            //System.out.println(w + "???");
            if(w.equals("Off"))
            {
                tmp = cin.next(); 
                //System.out.println(tmp);
                det = det.divide(BigInteger.valueOf(map.get(tmp)));
            }
            else
            {
                if(w.equals("On"))
                {
                    tmp = cin.next();
                    det = det.multiply(BigInteger.valueOf(map.get(tmp)));
                }
                else
                {
                    x = cin.nextBigInteger();
                    y = cin.nextBigInteger();
                }
            }
            if(p.compareTo(x.multiply(y).multiply(det).multiply(BigInteger.TEN)) == -1)
            {
                System.out.println("Slideshow");
            }
            else
            {
                if( p.compareTo(x.multiply(y).multiply(det).multiply(BigInteger.valueOf(60))) == -1)
                {
                    System.out.println("So-so");
                }
                else
                {
                    System.out.println("Perfect");
                }
            }
        }     
    }
}

取对数的做法:

Result  :  Accepted     Memory  :  10973 KB     Time  :  1671 ms

/*
 * Author: Gatevin
 * Created Time:  2014/8/31 13:53:44
 * File Name: A.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

map<string, int> M;
int a[100010];
int n,m;
string tmp1;
string tmp2;
int w,h,p;


int main()
{
    cin>>n;
    int k;
    for(int i = 1; i <= n; i++)
    {
        cin>>tmp1>>a[i];
        M[tmp1] = a[i];
    }
    cin>>w>>h>>p;
    k = w*h;
    double pp = log(p*1.0);
    double hh = 0;
    hh += log(k*1.0);
    for(int i = 1; i <= n; i++)
    {
        hh += log(a[i]*1.0);
    }
    if(pp < log(10.0) + hh)
    {
        printf("Slideshow\n");
    }
    else
    {
        if(pp < log(60.0) + hh)
        {
            printf("So-so\n");
        }
        else
        {
            printf("Perfect\n");
        }
    }
    cin>>m;
    while(m--)
    {
        cin>>tmp1;
        if(tmp1 == "Off")
        {
            cin>>tmp2;
            hh -= log(M[tmp2]*1.0);
        }
        else
        {
            if(tmp1 == "On")
            {
                cin>>tmp2;
                hh += log(M[tmp2]*1.0);
            }
            else
            {
                cin>>w>>h;
                hh -= log(k*1.0);
                hh += log(w*h*1.0);
                k = w*h;
            }
        }
        if(pp < log(10.0) + hh)
        {
            printf("Slideshow\n");
        }
        else
        {
            if(pp < log(60.0) + hh)
            {
                printf("So-so\n");
            }
            else
            {
                printf("Perfect\n");
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值