题意:https://nanti.jisuanke.com/t/41420
给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值。
思路:
从最大重量的石子开始背包,每次ans+=dp【j-v【i】】就行了。
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 #define rint register int 24 #define fo(a,b,c) for(rint a=b;a<=c;++a) 25 #define fr(a,b,c) for(rint a=b;a>=c;--a) 26 #define mem(a,b) memset(a,b,sizeof(a)) 27 #define pr printf 28 #define sc scanf 29 #define ls rt<<1 30 #define rs rt<<1|1 31 typedef long long ll; 32 void swapp(int &a,int &b); 33 double fabss(double a); 34 int maxx(int a,int b); 35 int minn(int a,int b); 36 int Del_bit_1(int n); 37 int lowbit(int n); 38 int abss(int a); 39 const double E=2.718281828; 40 const double PI=acos(-1.0); 41 //const ll INF=(1LL<<60); 42 const int inf=(1<<30); 43 const double ESP=1e-9; 44 const int mod=(int)1e9+7; 45 const int N=(int)1e6+10; 46 47 int v[N],dp[N]; 48 void Init(int _[],int n) 49 { 50 for(int i=1;i<=n;++i) 51 _[i]=0; 52 } 53 54 void PR(int _[],int n) 55 { 56 for(int i=1;i<=n;++i) 57 pr("%d ",_[i]); 58 pr("\n"); 59 } 60 61 bool J(int sum,int x,int min_) 62 { 63 return 2*x>=sum&&2*x<=sum+min_; 64 } 65 66 int main() 67 { 68 int T; 69 sc("%d",&T); 70 while(T--) 71 { 72 int n,sum=0; 73 sc("%d",&n); 74 for(int i=1;i<=n;++i) 75 sc("%d",&v[i]),sum+=v[i]; 76 sort(v+1,v+1+n); 77 Init(dp,sum); 78 79 ll ans=0; 80 dp[0]=1; 81 for(int i=n;i>=1;--i) 82 { 83 for(int j=sum;j>=v[i];--j) 84 if(dp[j-v[i]]) 85 { 86 dp[j]+=dp[j-v[i]]; 87 dp[j]%=mod; 88 ans+=J(sum,j,v[i])?dp[j-v[i]]:0; 89 ans%=mod; 90 } 91 } 92 pr("%lld\n",ans); 93 } 94 return 0; 95 } 96 97 /**************************************************************************************/ 98 99 int maxx(int a,int b) 100 { 101 return a>b?a:b; 102 } 103 104 void swapp(int &a,int &b) 105 { 106 a^=b^=a^=b; 107 } 108 109 int lowbit(int n) 110 { 111 return n&(-n); 112 } 113 114 int Del_bit_1(int n) 115 { 116 return n&(n-1); 117 } 118 119 int abss(int a) 120 { 121 return a>0?a:-a; 122 } 123 124 double fabss(double a) 125 { 126 return a>0?a:-a; 127 } 128 129 int minn(int a,int b) 130 { 131 return a<b?a:b; 132 }
原文地址:https://www.cnblogs.com/--HPY-7m/p/11536111.html
时间: 2024-11-05 18:41:00