【CodeForces - 618A】Slime Combining(二进制,思维)
題干:
Your friend recently gave you some slimes for your birthday. You have?n?slimes all initially with value?1.
You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other?n?-?1?slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value?v, you combine them together to create a slime with value?v?+?1.
You would like to see what the final state of the row is after you've added all?nslimes. Please print the values of the slimes in the row from left to right.
Input
The first line of the input will contain a single integer,?n?(1?≤?n?≤?100?000).
Output
Output a single line with?k?integers, where?k?is the number of slimes in the row after you've finished the procedure described in the problem statement. The?i-th of these numbers should be the value of the?i-th slime from the left.
Examples
Input
1Output
1Input
2Output
2Input
3Output
2 1Input
8Output
4Note
In the first sample, we only have a single slime with value?1. The final state of the board is just a single slime with value?1.
In the second sample, we perform the following steps:
Initially we place a single slime in a row by itself. Thus, row is initially?1.
Then, we will add another slime. The row is now?1 1. Since two rightmost slimes have the same values, we should replace these slimes with one with value?2. Thus, the final state of the board is?2.
In the third sample, after adding the first two slimes, our row is?2. After adding one more slime, the row becomes?2 1.
In the last sample, the steps look as follows:
題目大意:
朋友們送了一些史萊姆當作小N的生日禮物,總共有n個史萊姆,每個粘度為1。
小N打算用這些史萊姆玩游戲。他開始時先在桌子上放置一個史萊姆,然后逐個添加其他n-1個史萊姆。?
當添加史萊姆時,每次都將其放置在所有已放置的史萊姆的右側(cè)。 一旦出現(xiàn)兩個相鄰的史萊姆粘度相同均為v ,它們就將組合在一起變?yōu)橐粋€粘度v+1的史萊姆。
小N想知道在添加所有n個史萊姆之后,桌子上所有史萊姆的粘度是多少。 請從左到右打印史萊姆的粘度。
解題報告:
? ?不難發(fā)現(xiàn)規(guī)律是二進制中1的位置,然后亂搞就行了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 200 + 5; int ans[MAX]; int tot; int main() {int n;cin>>n;int cur = 0;while(n) {cur++;if(n&1) ans[++tot] = cur;n>>=1;}for(int i = tot; i>=1; i--) {printf("%d ",ans[i]);}return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 618A】Slime Combining(二进制,思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: supporter5.exe - sup
- 下一篇: 【HDU - 5965】扫雷(dp)