日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2019长安大学ACM校赛网络同步赛 BTrial of Devil (递归)

發布時間:2023/12/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2019长安大学ACM校赛网络同步赛 BTrial of Devil (递归) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/897/B
來源:牛客網

Trial of Devil 時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld

題目描述

As an acmer, Devil Aguin particularly loves numbers. This time, with a sequence consisting of n elements 1~n initially, Devil Aguin asks you to process the sequence until all the elements in it turn to zero. What you can do in one operation are as following :
????1. Select some elements from the sequence arbitrarily.
????2. Select a positive integer x arbitrarily.
????3. Subtract x from the elements you select.
It is obvious that there are various methods to make elements of the sequence turn to zero. But Devil Aguin demands of you to use the minimum operations. Please tell him how many operations you will use.

輸入描述:

????The first line contains an integer number T, the number of test cases.

? ??ithith of each next T lines contains one integer n(1n1001≤n≤100), the number of sequence.

輸出描述:

For each test case print a number, the minimum number of operations required. 示例1

輸入

復制 2 1 2

輸出

復制 1 2

題意:
給你一個t組數據,每一個數據給你一個整數n,
代表有一個n的全排列, 你可以做一些操作讓他們的值都變成0,。
每次操作,你可以選擇全排列中的一些數,然后再選擇任意一個x,讓那些數減去x。
現在問你最小的操作次數是多少?

思路:

對于每一個整數n的全排列,我們第一次就選擇大于等于n/2的那些數,然后減去n/2。
例如8
1 2 3 4 5 6 7 8
8/2=4
減去4后就是
1 2 3 0 1 2 3 4
因為處理1 2 3 4 的時候,就可以順帶的把1 2 3 也給處理了,所以我們問題就轉化為了 n/2的全排列的減為0的問題。
通過一直這樣轉為更小的全排列的操作,我們即可得到最優的解,當n=1的時候,只需要1步就可以減為0。
那么我們遞歸函數就可以寫成:
int f(int x) {if(x==1){return 1;}else{return 1+f(x/2);} }

AC代碼:

int f(int x) {if(x==1){return 1;}else{return 1+f(x/2);} } int main() {//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);int t;gbtb;cin>>t;while(t--){int n;cin>>n;if(n==1){cout<<1<<endl;}else{cout<<f(n)<<endl;}}return 0; }

?




轉載于:https://www.cnblogs.com/qieqiemin/p/10931300.html

總結

以上是生活随笔為你收集整理的2019长安大学ACM校赛网络同步赛 BTrial of Devil (递归)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。