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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【Gym - 101061F】Fairness(dp,思维)

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Gym - 101061F】Fairness(dp,思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Dwik and his brother Samir both received scholarships from a famous university in India. Their father, Besher, wants to send some money with each of them.

Besher has?n?coins, the?ith?coin has a value of?ai. He will distribute these coins between his two sons in?n?steps. In the?ith?step, he chooses whether to give the?ithcoin to Dwik or to Samir.

Let?xi?be the absolute difference between the sum of Dwik's and Samir's coins after the?ith?step. The unfairness factor of a distribution is?max({x1,?x2,?...,?xn}). Besher wants to minimize the unfairness factor, can you help him?

Input

The first line of the input consists of a single integer?t, the number of test cases. Each test case consists of 2 lines:

The first line contains an integer?n?(1?≤?n?≤?100).

The second line contains?n?integers?a1,?a2,?...,?an?(1?≤?ai?≤?100).

Output

Print?t?lines,?ith?line containing a single integer, the answer to the?ith?test case.

Example

Input

2 5 1 2 1 4 3 7 4 5 6 1 1 3 4

Output

2 5

Note

In the first sample test, besher has?5?coins (1,?2,?1,?4,?3), he can distribute them in the following way:

Step 1:?Give the first coin to dwik ,?d?=?1,?s?=?0??x1?=?|1?-?0|?=?1

Step 2:?Give the second coin to samir,?d?=?1,?s?=?2??x2?=?|1?-?2|?=?1

Step 3:?Give the third coin to samir,?d?=?1,?s?=?3??x3?=?|1?-?3|?=?2

Step 4:?Give the fourth coin to dwik,?d?=?5,?s?=?3??x4?=?|5?-?3|?=?2

Step 5:?Give the fifth coin to samir,?d?=?5,?s?=?6??x5?=?|5?-?6|?=?1

max({x1,?x2,?x3,?x4,?x5})?=?2

解題報告:

直接dp[i][j]代表前i個物品其中A這個人選擇了總價值為j的容量的最小化最大差值。

注意代碼邏輯,所以要算兩個轉移的時候分別取最大值,然后取個最小值賦值給dp[i][j],

而不能每次都dp[i][j]=min(dp[i][j],max())。其實如果用我為人人的寫法的話就可以避免這個問題。

(然而好像也可以直接每次都更新dp[i][j]呀?)

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e4 + 5; int dp[105][MAX],a[MAX],sum[MAX]; int ZERO = 1e4,n; int main() {int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i),sum[i] = sum[i-1] + a[i];memset(dp,0x3f,sizeof dp);int inf = dp[0][0];dp[0][0]=0;for(int i = 1; i<=n; i++) {for(int j = 0; j<=sum[n]; j++) {int curA = j,curB = sum[i] - j;int cA = j,cB = sum[i] - cA;int tmp1=0x3f3f3f3f,tmp2=0x3f3f3f3f;tmp1 = max(dp[i-1][j],abs(curA-curB));//把第i個放入B中 if(j >= a[i])tmp2 = max(dp[i-1][j-a[i]],abs(cA-cB));//把第i個放入A中 dp[i][j] = min(tmp1,tmp2);}}int ans = 0x3f3f3f3f;for(int i = 0; i<MAX; i++) ans = min(ans,dp[n][i]);printf("%d\n",ans);} return 0 ; }

這題還可以用記憶化或者二分寫,思路差不多,光貼個代碼吧:

總結

以上是生活随笔為你收集整理的【Gym - 101061F】Fairness(dp,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

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