您的位置:首页 > 其它

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

2014-09-03 12:00 232 查看
题目大意:

现在给出一个游戏中的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息