CodeForces 626C

2503 단어

Block Towers
time limit: per test
2 seconds
memory limit: per test
256 megabytes
input:
standard input
output:
standard output
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.
The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.
Input
The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.
Output
Print a single integer, denoting the minimum possible height of the tallest tower.
Examples
input
1 3
output
9
input
3 2
output
8
input
5 0
output
10
Note
In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.
In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks. 제목은 높이가 2인 벽돌을 사용할 수 있는 아이가 n명, 높이가 3인 벽돌을 사용할 수 있는 아이가 m명 있다. 그들은 자신을 위해 높이가 h인 탑을 만들 것이다. 그러나 아이들의 탑 높이는 같을 수 없다. 지금 너에게 n과 m을 주겠다. 최소 높이가 얼마냐고 묻는다.문제풀이 사고방식: 숫자 i 앞부분(i 포함)은 2 또는 3의 배수인 개수는 i/2+i/3-i/6개이고 가장 작은 탑은 max(2*n, 3*m)이며 최대 탑은 3000000(300000에서 2 또는 3배수는 1500000+100000000-500000=2000000)이기 때문에 이 두 개 수 사이에서 조건에 맞는 최소한의 탑 높이를 찾아내면 된다.
조건: 1.n+m는 반드시 n+m개가 2 또는 3의 배수인 것을 보증한다.h/2>=n은 전 h개수에 n개 2의 배수가 있음을 보증해야 한다.h/3>=m는 앞의 h개수가 m개3의 배수인 2000000도 크지 않고 직접적으로 폭력적일 수 있지만 2000000000을 주면 GG가 되기 때문에 이분법을 사용한다.
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int l=max(2*n,3*m),r=3000005;
    while(l<r)	
    {
         int y=(l+r)/2;
        if(y/2+y/3-y/6>=n+m&&y/2>=n&&y/3>=m)r=y;
        else l=y+1;
    }
    printf("%d
",l); return 0; }

좋은 웹페이지 즐겨찾기