zoukankan      html  css  js  c++  java
  • UVA

    Dr. Tuple is working on the new data-mining application for Advanced Commercial Merchandise Inc. One of the subroutines for this application works with two arrays P and Q containing N records of data each (records are numbered from 0 to N - 1). Array P contains hash-like structure with keys. ArrayP is used to locate record for processing and the data for the corresponding record is later retrieved from the array Q.

    All records in array P have a size of SP bytes and records in array Q have size of SQ bytes. Dr. Tuple needs to implement this subroutine with the highest possible performance because it is a hot-spot of the whole data-mining application. However, SP and SQ are only known at run-time of application which complicates or makes impossible to make certain well-known compile-time optimizations.

    The straightforward way to find byte-offset of i-th record in array P is to use the following formula:

    Pofs(i) = SP . i, (1)

    and the following formula for array Q:

    Qofs(i) = SQ . i. (2)

    However, multiplication computes much slower than addition or subtraction in modern processors. Dr. Tuple avoids usage of multiplication while scanning array P by keeping computed byte-offset Pofs(i) of i-th record instead of its index i in all other data-structures of data-mining application. He uses the following simple formulae when he needs to compute byte-offset of the record that precedes or follows i-th record in array P:

    Pofs( i + 1) = Pofs( i) + SP

    Pofs( i - 1) = Pofs( i) - SP

    Whenever a record from array P is located by either scanning of the array or by taking Pofs(i) from other data structures, Dr. Tuple needs to retrieve information from the corresponding record in array Q. To access record in array Q its byte-offset Qofs(i) needs to be computed. One can immediately derive formula to compute Qofs(i) with known Pofs(i) from formulae (1) and (2):

    Qofs(i) = Pofs(i)/SP . SQ (3)

    Unfortunately, this formula not only contains multiplication, but also contains division. Even though only integer division is required here, it is still an order of magnitude slower than multiplication on modern processors. If coded this way, its computation is going to consume the most of CPU time in data-mining application for ACM Inc.

    After some research Dr. Tuple has discovered that he can replace formula (3) with the following fast formula:

    Qofs'(i) = (Pofs(i) + Pofs(i) < < A) > > B (4)

    where A and B are non-negative integer numbers, `` < < A" is left shift by A bits (equivalent to integer multiplication by 2A), `` > > B" is right shift by Bbits (equivalent to integer division by 2B).

    This formula is an order of magnitude faster than (3) to compute, but it generally cannot always produce the same result as (3) regardless of the choice for values of A and B. It still can be used if one is willing to sacrifice some extra memory.

    Conventional layout of array Q in memory (using formula (2)) requires N . SQ bytes to store the entire array. Dr. Tuple has found that one can always choose such K that if he allocates K bytes of memory for the array Q (where K$ le$N . SQ) and carefully selects values for A and B, the fast formula (4) will give non-overlapping storage locations for each of the N records of array Q.

    Your task is to write a program that finds minimal possible amount of memory K that needs to be allocated for array Q when formula (4) is used. Corresponding values for A and B are also to be found. If multiple pairs of values for A and B give the same minimal amount of memory K, then the pair where A is minimal have to be found, and if there is still several possibilities, the one where B is minimal. You shall assume that integer registers that will be used to compute formula (4) are wide enough so that overflow will never occur.

    Input 

    Input consists of several datasets. Each dataset consists of three integer numbers NSP , and SQ separated by spaces (1$ le$N$ le$220, 1$ le$SP$ le$210, 1$ le$SQ$ le$210).

    Output 

    For each dataset, write to the output a single line with three integer numbers KA, and B separated by spaces.

    Sample Input 

    1024 7 1
    

    Sample Output 

    1119 2 5
    
    
    题目不太好理解,要深入多读几遍,要注意计算K的方法
    实际上就是暴力,要注意A和B的暴力范围和K的初始值
    
    
    
    #include <bits/stdc++.h>
    using namespace std;
    
    long long int n, sp, sq;
    int A, B;
    
    long long int getK(int a, int b)
    {
        return (((n-1)*sp + (((n-1)*sp)<<a)) >> b) + sq;
    }
    
    int main()
    {
        while(cin >> n >> sp >> sq)
        {
            long long int k, mink = pow(2, 60);
            int a = 0, b = 0;
            for(A = 0; A<=31; A++)
            {
                for(B = 0; B<=31; B++)
                {
                    k = getK(A, B);
                    if(k < mink && k>=n*sq)
                        mink = k, a = A, b = B;
                }
            }
            cout << mink << " " << a << " " << b << endl;
        }
        return 0;
    }


  • 相关阅读:
    679 怎样杀死害虫?(对付一个系统最好的方式是“围城必阙”)
    678 "流浪地球"为什么是个好地方?(系统越复杂拥有好运气的机会也就越大)
    677 人类为什么会养猫?(做一件事理性的原因的背后往往还隐藏着自己都不曾发现的感性原因)
    职场人必知的三原则
    677 怎样当一个少数派?(越在意,越出众)
    675 为什么会有“黑天鹅”?(行为和对行为后果的负责与否决定了很多黑天鹅出现概率)
    不做特殊论者(没有所谓的理所当然,你所谓的成功很有可能只是因为运气)
    事实和观点(就事论事,事实有真假,观点无对错)
    一个程序员的价值观总结
    669 创新也是搞政治?(如何创新)
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312789.html
Copyright © 2011-2022 走看看