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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

A Boring Game

發布時間:2023/12/3 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 A Boring Game 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題面: Jeff has got?2n2n?real numbers?a1,a2,,a2na1,?a2,?…,?a2n. He decides to adjust the numbers. Namely, Jeff consecutively executes?nn?operations, each of them goes as follows: choose indexes?ii?and?jj?(iji?≠?j) that haven't been chosen yet; round element?aiai?to the nearest integer that is no more than?aiai. round element?ajaj?to the nearest integer that is no less than?ajaj.

Jeff wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help him find the minimum absolute value of the difference.

輸入:

In the first line is a number?TT?(T100T≤100) indicating the cases following. In each case, the first line contains an integer?nn?(1n20001≤?n?≤?2000). The next line contains?2n2n?real numbers?a1,a2,,a2na1,a2,…,a2n?(0ai100000?≤?ai?≤?10000), given with exactly three digits after the decimal point. The numbers are separated by spaces. 輸出:

Print?TT?lines. In each line print a single real number — the required difference with exactly three digits after the decimal point.

樣例:

1 3 1.500 1.750 2.000 3.000 4.000 5.000 樣例輸出:

0.250

題目大意:給定2n個小數,并執行n次操作,每次操作同時操作兩個數,把其中的一個向上取整,另一個向下取整。問,在執行完操作以后數列的和與執行操作以前數列的和差值最小是多少。

題解:

這個題實際上也并不難,但比賽時候很多人沒做出來。

思路是這樣的,因為x.000這樣的數不論向上取整還是向下取整都是一樣的,所以我們可以不考慮這些數的求和,而僅僅是統計這些數的個數,假設他們的總數為cnt。

而對于其他的小數來說,如果向上取整,那么相當于+1,如果向下取整,相當于不加。

因此我們用所有小數部分的和表示操作以前的數的和,例如sum1 = 0.500+0.750 = 1.250

而用向上取整的小數的個數代表操作后的小數的和,例如sum2 = 1或sum2 = 2或sum2 = 0

這樣的話差值就是sum1-sum2了,我們只要求出sum2中可以向上取整的小數個數的范圍就行了。

顯然,范圍是max(0,n-cnt) 到 min(n,2*n-cnt)

AC代碼:

#include <iostream> #include <cstdio> using namespace std; const double INF = 1e9; double iabs(double x) {return x>0?x:-x;} int main() {int T;scanf("%d",&T);while(T--){int cnt = 0;double sum = 0;int n;scanf("%d",&n);for(int i = 0;i < 2*n;i++){double d;scanf("%lf",&d);double x = d - int(d);if(x < 0.000001) cnt++;sum += x;}double ans = INF; for(int i = max(0,n-cnt);i <= min(2*n-cnt,n);i++){ans = min(ans,iabs(sum - (double)i));}printf("%.3lf\n",iabs(ans));}return 0; }



總結

以上是生活随笔為你收集整理的A Boring Game的全部內容,希望文章能夠幫你解決所遇到的問題。

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