Codeforces 268B - Buttons
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.
A single line contains integer n (1 ≤ n ≤ 2000) — the number of buttons the lock has.
In a single line print the number of times Manao has to push a button in the worst-case scenario.
2
3
3
7
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个按钮,必须要按照顺序按下去才会把一个锁打开,否则一步按错将会重置所有按钮,题目要求的就是在最坏情况下最多按多少次按钮就能把锁打开。考虑两个按钮的情况,如果顺序是先按2再按1,最坏的情况是先按1,发现重置了,再按2,可以,再按1,总共三步锁打开了。
下面拓展到n个情况,设每一轮我们都按下去一个按钮,假设我们都是在最后才按到正确的按钮上
第一轮,我们最多按n个按钮
第i轮(i<=n),我们已经按下去了i-1个按钮,剩下n-i+1个按钮要按,这一步我们需要找到第i个正确的按钮。依次判断剩下的按钮中某个按钮是不是正确,判断第一个按钮我们只需要1次,不过判断第2个按钮我们需要把之前i-1个按钮都按下再加上第2个按钮,剩下的依次类推,把上次按错按钮导致的重置步数加到确认下一个按钮的步数中,直到我们找到正确的按钮,这时需要按 (n-i)*i次。所以,第i轮总共需要按 1+(n-i)*i次按钮。
i从1到n,加起来就是题目中最多按按钮的次数。
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define lowbit(x) x&(-x)
int main()
{
int n,s;
while(cin>>n){
s=;
for(int i=;i<=n;i++)
s+=+(n-i)*i;
cout<<s<<endl;
}
return ;
}
Codeforces 268B - Buttons的更多相关文章
- codeforces 520 Two Buttons
http://codeforces.com/problemset/problem/520/B B. Two Buttons time limit per test 2 seconds memory l ...
- CodeForces 520B Two Buttons(用BFS)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round B. Buttons
Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you ...
- Codeforces 520B:Two Buttons(思维,好题)
题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...
- Codeforces Round #295 (Div. 2)B - Two Buttons BFS
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )
B. Two Buttons time limit per test : 2 seconds memory limit per test :256 megabytes input :standard ...
- Codeforces Round #295 (Div. 2) B. Two Buttons
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 【codeforces 520B】Two Buttons
[题目链接]:http://codeforces.com/contest/520/problem/B [题意] 给你一个数n; 对它进行乘2操作,或者是-1操作; 然后问你到达m需要的步骤数; [题解 ...
- Codeforces Round #295 (Div. 2) B. Two Buttons 520B
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- 查看Centos内存使用情况linux命令
我们在使用centos版linux服务器的过程中,有时会出现卡顿的情况,这时我们可以通过查看一下内存的使用来判断发生了什么情况,那么如何查看centos内容使用情况呢?有几个方法可以尝试,跟着ytka ...
- Codefoces 432C Prime Swaps(数论+贪心)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...
- 微星笔记本每次都进bios
解决方法 bios中更改启动模式,要更改为LEGACY
- 快学Scala 2
控制结构和函数 1.在Scala中,几乎所有构造出来的语法结构都有值.这个特性是为了使得程序更加精简,也更易读. (1)if表达式有值 (2)块也有值——是它最后一个表达式的值 (3)Scala的fo ...
- from C++ to Java
绝大部分对象都是指针,创建对象习惯性用new const -> final 枚举类型 与 int的相互转换: 从int到enum: MyEnum.values()[x], where x mu ...
- drf频率组件
1.简介 控制访问频率的组件 2.使用 手写一个自定义频率组件 import time #频率限制 #自定义频率组件,return True则可以访问,return False则不能访问 class ...
- Spark算子之aggregateByKey详解
一.基本介绍 rdd.aggregateByKey(3, seqFunc, combFunc) 其中第一个函数是初始值 3代表每次分完组之后的每个组的初始值. seqFunc代表combine的聚合逻 ...
- centos6下升级git版本的操作记录
编译go_ethereum的时候出现了错误 然后发现是自己的git没有升级成功 因为编译需要高版本的git版本 所以会编译不成功 之后执行 root@uatjenkins01 ~]# git - ...
- pycharm tips
批量更改变量名,就在该变量名上shift+f6 ../data 两个点,就是上一级目录,一个点就是当前目录 unhashable type: 'list' 使用set进行去重 a = [1,2,2,3 ...
- 日期格式化(类似QQ邮箱中的邮件列表显示日期)
日期格式化(类似QQ邮箱中的邮件列表显示日期) public static string FormatDateDisplay(DateTime _datetime) { var ts = DateTi ...