16行代码AC_Keeping Rabbits Gym - 102394K(附超时原因)
勵志用少的代碼做高效表達
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個兔子, 每個兔子都有一個體重, 每天給這n個兔子喂一個胡蘿卜, 體重越大,吃到胡蘿卜的概率就越大, 問經過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
也就是說:兔子體重的增加,并不會改變他們后續吃到蘿卜的比重。 這個比例是一個定值。 那么,本題就可以轉化為: 求每個兔子占所有兔子體重的比例, 將這個比例乘以天數,加上原始體重,將得到的結果輸出即可。
注意
最初提交時,case5總是超時, 我們百思不得其解,而后嘗試將for循環的cin換為sacnf, 時間規模直接下降了500ms! 成功AC。
可見cin和scanf的速度差距還是蠻大的。
將這一點寫出來, 防止大家踩雷。
代碼展示
#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;}努力只能及格,拼命才能優秀! 加油,陌生人!
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的16行代码AC_Keeping Rabbits Gym - 102394K(附超时原因)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 13行代码AC_Justifying t
- 下一篇: 25行代码AC_蓝桥杯 2017A组省赛