TOJ-3474 The Big Dance(递归二分)
生活随笔
收集整理的這篇文章主要介紹了
TOJ-3474 The Big Dance(递归二分)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
?
鏈接:https://ac.nowcoder.com/acm/contest/1077/L
題目描述
Bessie and the herd, N (1 <= N <= 2,200) conveniently numbered 1..N cows in all, have gone to a dance where plenty of bulls are available as dancing partners. This dance is known as the "odd cow out" dance because of the way cows are chosen to dance with bulls.The cows are lined up in numerical order and the 'middle' point is chosen. It either divides the line of cows exactly in half or it is chosen so that the first set of cows has just one more cow in it than the second set. If exactly two cows are in the set, they are chosen to dance with bulls. If one cow is in the set, she is sent home with a consolation prize of a beautiful rose.
If the set has more than two cows in it, the process is repeated perhaps again and again until sets with just one or two cows emerge.
The two cow ID numbers are multiplied together and added to a global sum.
Given the number of cows at the dance, compute the global sum after all the eligible cows are chosen. Consider a dance with 11 cows numbered 1..11. Here is the sequence of dividing them: 1 2 3 4 5 6 | 7 8 9 10 111 2 3 | 4 5 61 2 | 31 2 => 1*2=2 added to sum -> sum=23 => sent home with rose4 5 | 64 5 => 4*5=20 added to sum -> sum=226 => sent home with rose7 8 9 | 10 117 8 | 97 8 => 7*8=56 added to sum -> sum=789 => sent home with rose10 11 => 10*11=110 added to sum -> sum=188So the sum for this dance would be 188.
輸入描述:
* Line 1: A single integer: N
輸出描述:
* Line 1: A single integer that is the sum computed as prescribed.
示例1
輸入
11輸出
188?
簡(jiǎn)單二分,直接粘代碼
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <map> 11 #include <math.h> 12 const int INF=0x3f3f3f3f; 13 typedef long long LL; 14 const int mod=1e9+7; 15 const double PI=acos(-1); 16 const int maxn=100010; 17 using namespace std; 18 //ios::sync_with_stdio(false); 19 // cin.tie(NULL); 20 21 int n,ans; 22 23 void solve(int l,int r) 24 { 25 if(l==r) 26 return ; 27 if(l+1==r) 28 { 29 ans+=l*r; 30 return ; 31 } 32 int mid=(l+r)>>1; 33 solve(l,mid); 34 solve(mid+1,r); 35 return ; 36 } 37 38 int main() 39 { 40 scanf("%d",&n); 41 solve(1,n); 42 printf("%d\n",ans); 43 return 0; 44 }?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/jiamian/p/11382986.html
總結(jié)
以上是生活随笔為你收集整理的TOJ-3474 The Big Dance(递归二分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android小作业,Android s
- 下一篇: 鼠标出舞台,“手放开”