LA 7500 Boxes and Balls (数学)

题意:给定 n 个球,每次从每篮子里拿出来一个放在一个新篮子里,并移除相同的,按球的个数进行排序,问你用最多几个球能完成循环。

析:数学问题,很容易发现前n项和就是最多的球数,所以我们只要找最大的n项就好了。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef unsigned long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};

int main(){
    int T;  cin >> T;
    LL x;
    for(int kase = 1; kase <= T; ++kase){
        cin >> x;
        LL y = (LL)sqrt(2.0*x*1.0);
        LL ans = y*(y-1)/2;
        for(LL i = y-1; ; ++i){
            if(i * (i+1) <= 2*x)  ans = max(ans, i*(i+1)/2);
            else break;
        }
        printf("Case #%d: ", kase);
        cout << ans << endl;
    }
    return 0;
}
时间: 2024-10-09 15:53:54

LA 7500 Boxes and Balls (数学)的相关文章

UVALive 7500 Boxes and Balls 2015EC final 签到题 二分

分析题目后,得到要求的是最接近n的一个数,并且这个数字能写成1+2+3+....+x = ans这种形式. 要求的是最大的值. 这题就直接二分去做吧.二分出一个f(mid)<=n的最大值. 最后的end就是所求的f(end) 为什么呢?,我来分析下我这个二分是怎么实现的 while (begin<=end) { LL mid = (begin + end) / 2; if (f(mid) == n) { printf ("Case #%d: %lld\n",++ff,n);

uvalive 7500 Boxes and Balls

https://vjudge.net/problem/UVALive-7500 题意: 找到规律之后发现给出一个数n,要求找到1 + 2i + ... + x <= n,找出1到x的和. 思路: 一看n就知道要二分,还以为是二分写炸了...结果是r的范围太小了,因为n最大是1e18,又有除以2,所以r应该是2e9才对,不是1e9. 代码: 1 #include <stdio.h> 2 3 int main() 4 { 5 int t; 6 7 scanf("%d",&

【CF884D】Boxes And Balls 哈夫曼树

[CF884D]Boxes And Balls 题意:有n个箱子和若干个球,球的颜色也是1-n,有ai个球颜色为i,一开始所有的球都在1号箱子里,你每次可以进行如下操作: 选择1个箱子,将里面所有的球拿出来,分成k部分(你可以令k=2或3),将每一部分都放到一个空箱子中.花费的代价是这个箱子中球的总数. 现要求你在若干次操作后,所有颜色为i的球都在i号箱子里,求最小代价. n<=200000,ai<=10^9 题解:傻逼题都不会做了.将操作反过来,就变成了将k个箱子合并到一起,这就变成经典的哈

Boxes and Balls UVALive - 7500(练习赛爆零)

原因: 自身: 1.自己并没有考虑过精度所带来的问题. 2.一定要自己读题,独立思考,末被队友带偏(矛盾出真理). 3.加强自身基础,提高自身实力. 队伍: 1.队友缺乏独立思考,需要加强. 题目描述: 给你n个球,求在有限次数变化中,球的变化最后稳定在一种状态. 思路: 打表找过规律后发现,稳定状态下球的个数是1,2,3,6,10,15,21.....是以等差数列的前n项和.s=(n+1)*n/2; 思路一:二分模拟(想到了,但是深度不够),没有尝试. 思路二:借二元一次方程.(x+1)*x-

数学类杂志SCI2013-2014影响因子

ISSN Abbreviated Journal Title Full Title Category Subcategory Country total Cites IF        2013-2014 IF 2012-2013 IF 2011-2012 IF 2010-2011 IF 2009-2010 IF 2008-2009 IF 2007-2008 5-Year Impact Factor Immediacy Index Articles Cited Half-Life Eigenfa

2015 ACM/ICPC EC-Final

A. Boxes and Balls 二分找到最大的不超过$n$的$\frac{x(x+1)}{2}$形式的数即可. #include <bits/stdc++.h> using namespace std ; typedef long long LL ; void solve () { LL n ; scanf ( "%lld" , &n ) ; LL l = 1 , r = 2e9 ; while ( l < r ) { LL m = l + r + 1

UVA - 12293 Box Game (规律)

Description  Box Game  There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves alternatively, Alice mo

UVA 12293 Box Game(博弈入门)

题目链接:Box Game 题面:            12293 Box Game There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves al

Box Game

There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves alternatively, Alice moves first. For each mov