zoukankan      html  css  js  c++  java
  • codeforces-268B

    B. Buttons
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in a certain order to open the lock. When you push some button, it either stays pressed into the lock (that means that you've guessed correctly and pushed the button that goes next in the sequence), or all pressed buttons return to the initial position. When all buttons are pressed into the lock at once, the lock opens.

    Consider an example with three buttons. Let's say that the opening sequence is: {2, 3, 1}. If you first press buttons 1 or 3, the buttons unpress immediately. If you first press button 2, it stays pressed. If you press 1 after 2, all buttons unpress. If you press 3 after 2, buttons 3 and 2 stay pressed. As soon as you've got two pressed buttons, you only need to press button 1 to open the lock.

    Manao doesn't know the opening sequence. But he is really smart and he is going to act in the optimal way. Calculate the number of times he's got to push a button in order to open the lock in the worst-case scenario.

    Input

    A single line contains integer n (1 ≤ n ≤ 2000) — the number of buttons the lock has.

    Output

    In a single line print the number of times Manao has to push a button in the worst-case scenario.

    Examples
    input
    2
    output
    3
    input
    3
    output
    7
    Note

    Consider the first test sample. Manao can fail his first push and push the wrong button. In this case he will already be able to guess the right one with his second push. And his third push will push the second right button. Thus, in the worst-case scenario he will only need 3 pushes.

    这道题的意思大致为:输入数字n,表示n个按钮,需要按顺序全部按对才会成功,按错时会清空,输出最多按多少次按钮

    5个按钮时,按钮次数参考下图

    4  3  2  1

    3  2  1

    2  1

    1

    理解起来为第一个按钮不确定时需要按4次确定第一个按钮,然后按3次确定第二个按钮,期间第一个按钮按了3次,以此类推,最后争取的按钮按了n次。

    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        int sum1 = 0, sum = 0;
        for (int i = 1;i < n;i++)
        {
            sum1 += i;
            sum += sum1;
        }
        sum += n;
        cout << sum << endl;
        return 0;
    }
  • 相关阅读:
    Hibernate Validator
    RocketMQ之八:重试队列,死信队列,消息轨迹
    使用hibernate validator出现
    Hibernate Validator--创建自己的约束规则
    Java应用中使用ShutdownHook友好地清理现场、退出JVM的2种方法
    笔者带你剖析轻量级Sharding中间件——Kratos1.x
    [caffe]深度学习之图像分类模型VGG解读
    类的载入机制
    机器人api(图灵机器人)
    回文串问题总结
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/7885747.html
Copyright © 2011-2022 走看看