TC srm.673 300
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
Description
给你n(n<=50)匹马和n个人,一匹马和一个人能够组成一个骑兵,这个骑兵的战斗力等于马的战斗力乘以人的战斗力,问你有多少种组合方式满足其他骑兵的战斗力都不超过第0号骑兵。
Input
Output
Sample Input
Sample Output
HINT
题意
题解:
大概就暴力枚举哪匹马和第0号人匹配,后面的骑兵我们按照战斗力从大到小排序之后,对于每一个骑兵二分(或者暴力)找到最多可以选多少匹马,然后就可以容斥做一下就好了~
代码
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int mod = 1e9+7; bool cmp(int a,int b) { return a>b; } class BearCavalry{ public: int countAssignments(vector <int> warriors, vector <int> horses) { sort(warriors.begin()+1,warriors.end(),cmp); sort(horses.begin(),horses.end()); long long ans = 0; for(int i=0;i<horses.size();i++) { sort(horses.begin(),horses.end()); int p = warriors[0]*horses[i]; int k = horses[i]; horses.erase(horses.begin()+i); long long ans2 = 1; int flag = 0; for(int j=1;j<warriors.size();j++) { long long tmp = -1; for(int t=0;t<horses.size();t++) { if(warriors[j]*horses[t]>=p) break; tmp = t; } //cout<<i<<" "<<j<<" "<<tmp<<endl; tmp = tmp + 2 - j; //cout<<i<<" "<<j<<" "<<tmp<<endl; if(tmp<=0) { flag = 1; break; } ans2 = (ans2 * tmp) % mod; } if(flag==0) ans = (ans + ans2) % mod; horses.push_back(k); } return ans; } }; int main() { BearCavalry C; vector<int> a; vector<int> b; a.push_back(5);a.push_back(8);a.push_back(4);a.push_back(8); b.push_back(19);b.push_back(40);b.push_back(25);b.push_back(20); printf("%d\n",C.countAssignments(a,b)); }
时间: 2024-10-27 07:30:56