Ural 1011. Conductors

1011. Conductors

Time limit: 2.0 second
Memory limit: 64 MB

Background

Everyone making translations from English to Russian knows an English phrase "Naked conductor runs along the bus". It has two very different meanings.

Problem

Every bus in the Ekaterinburg city has a special man (or woman) called conductor. When you ride the bus, you have to give money to the conductor. We know that there are more than P% conductors and less than Q% conductors of all citizens of Ekaterinburg. Your task is to determine a minimal possible number of Ekaterinburg citizens. By percentage, we know that there are more than P% conductors and less than Q% conductors of all Russian citizens in this city

Input

Two numbers P,Q such that 0.01 ≤ PQ ≤ 99.99. Numbers are given with 2 digits precision. These numbers are separated by some spaces or "end of line" symbols.

Output

The minimal number of Ekaterinburg citizens.

Sample

input output
13
14.1
15

Notes

If there are 15 citizens and 2 conductors among them in Ekaterinburg, then there are 13 1/3 % conductors of all citizens.

Problem Source: USU Championship 1997

来源: <http://acm.timus.ru/problem.aspx?space=1&num=1011>

初步想法:从 1 开始遍历,当 n/p 和 n/q 之间存在整数,则所夹最小整数就是所求结果了。但下面的程序 floor 、 ceil 、 fabs 太耗时间了,虽然在自己电脑上都是秒出,但在评测机上达到了 2s 多,华丽丽 TLE。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){
    long n=0;
    double p,q,tp,tq;
    scanf("%lf%lf",&p,&q);
    p/=100.0;q/=100.0;
    while(++n){
        tp=floor(n/p);
        tq=ceil(n/q);
        if(fabs(tq-tp)<1E-2) break;
        //if(tp>tq) break;
        //if(tp<tq) continue;
    }
    printf("%.0f",tp);
    return 0;
}

?改进:整个算法就循环最耗时间了,有针对性地修改即可,直接 long 强转只保留整数部分,于是考虑:

?  n/p n/q  强转 ->  tp  tq

2.x  1.y                 2    1

3.x  1.y                 3    1

2.x  2.y                 2    2

可以看到上面就是小数直接夹着整数的情况成立条件是 tp > tq 。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

const double e=1E-6;
int main(){
    long n=0;
    double p,q;
    long tp,tq;
    scanf("%lf%lf",&p,&q);
    p/=100.0;q/=100.0;
    while(++n){
        //tp=long(n/p);  //WA
        //tq=long(n/q);
        tp=long(n/p-e);
        tq=long(n/q+e);
        if(tq<tp) break;
    }
    printf("%ld\n",tq+1);
    return 0;
}

By Black Storm(使用为知笔记)

时间: 2025-02-01 06:53:42

Ural 1011. Conductors的相关文章

ACM-ICPC(11/8)

URAL 1005 给定一些石头的重量,要求分成两个部分最相近.二进制暴力枚举. #include <bits/stdc++.h> using namespace std; int w[25]; int main() { // freopen("in.txt","r",stdin); int n; scanf("%d",&n); int sum = 0; for(int i = 0; i < n; i++) { scan

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

POJ 1011 - Sticks DFS+剪枝

POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    2. 从大到小枚举,因为小长度更灵活, 可拼接可不拼接    3. 因为每一跟木条都要用到, 故若轮到其中一根原始木段选它的第一根木条时,若第一根木条若不满足,则显然第一根木条在接下来任何一根原始木段都不会满足,故无解    4. 由于所有棒子已排序,在DFS时,若某根棒子未被选,则跳过其后面所有与

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar