16行代码AC_Keeping Rabbits Gym - 102394K(附超时原因)
勵(lì)志用少的代碼做高效表達(dá)
Problem description
DreamGrid is the keeper of n rabbits. Initially, the i-th (1≤i≤n) rabbit has a weight of wi.
Every morning, DreamGrid gives the rabbits a carrot of weight 1 and the rabbits fight for the only carrot. Only one rabbit wins the fight and eats the carrot. After that, the winner’s weight increases by 1. The whole process of fighting and eating ends before the next morning.
DreamGrid finds that the heavier a rabbit is, the easier it is to win a fight. Formally, if the weights of the rabbits are w′1,w′2,…,w′n before a fight, the probability that the i-th rabbit wins the fight is
He wants to know the expected weight of every rabbit after k days (k carrots are given and eaten).
Input
The input contains multiple cases. The first line of the input contains a single integer T (1≤T≤105), the number of cases.
For each case, the first line of the input contains two integers n and k (1≤n≤105,1≤k≤109). The second line contains n integers w1,w2,…,wn (1≤i≤n,1≤wi≤109).
It’s guaranteed that the sum of n over all cases doesn’t exceed 106.
Output
For each case, print a single line containing n space-separated real numbers, where the i-th (1≤i≤n) number should be equal to the expected weight of the i-th rabbit after k days.
Your answer will be considered correct if the absolute or relative error does not exceed 10?4.
題目(提交)鏈接——>傳送門
思路與題解
題意:n個(gè)兔子, 每個(gè)兔子都有一個(gè)體重, 每天給這n個(gè)兔子喂一個(gè)胡蘿卜, 體重越大,吃到胡蘿卜的概率就越大, 問經(jīng)過m天后這些兔子的重量是多少。
舉例:兔子甲重量為1, 兔子乙重量為3, 喂兩天的胡蘿卜, 兔子甲的體重占總比例的1/4, 那么兔子甲每天得到的胡蘿卜都是1/4, 兔子乙同理, 兩天后,
兔子甲的體重為:1+1/4+1/4=1.5兔子甲的體重為: 1+1/4+1/4= 1.5 兔子甲的體重為:1+1/4+1/4=1.5
兔子乙的體重為:3+3/4+3/4=4.5兔子乙的體重為: 3+3/4+3/4=4.5兔子乙的體重為:3+3/4+3/4=4.5
也就是說:兔子體重的增加,并不會(huì)改變他們后續(xù)吃到蘿卜的比重。 這個(gè)比例是一個(gè)定值。 那么,本題就可以轉(zhuǎn)化為: 求每個(gè)兔子占所有兔子體重的比例, 將這個(gè)比例乘以天數(shù),加上原始體重,將得到的結(jié)果輸出即可。
注意
最初提交時(shí),case5總是超時(shí), 我們百思不得其解,而后嘗試將for循環(huán)的cin換為sacnf, 時(shí)間規(guī)模直接下降了500ms! 成功AC。
可見cin和scanf的速度差距還是蠻大的。
將這一點(diǎn)寫出來, 防止大家踩雷。
代碼展示
#include<iostream> using namespace std; long long r[100100]; int main(){int t; cin>>t; while(t--){long long n, k; cin>>n>>k;long long sum=0;for(int i=0;i<n;i++){scanf("%lld", &r[i]);sum = sum+r[i];}for(int i = 0; i < n; i++) printf("%.8lf ", k*r[i]*1.0/sum+r[i]); printf("\n");} return 0;}努力只能及格,拼命才能優(yōu)秀! 加油,陌生人!
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的16行代码AC_Keeping Rabbits Gym - 102394K(附超时原因)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 13行代码AC_Justifying t
- 下一篇: 25行代码AC_蓝桥杯 2017A组省赛