zzulioj - 2617 体检
题目链接:http://acm.zzuli.edu.cn/problem.php?id=2617
这个题本来想着靠排序过的,结果思来想去死活过不了只好用全排列暴力过了,关于全排列算法请自行百度,这里不再赘述(其实我也是现学的)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cctype> #include<string> #include<vector> #include<climits> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define mst(a) memset(a, 0, sizeof(a)) #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n") using namespace std; typedef long long ll; typedef pair< int , int > P; const double eps = 1e-7; const int INF = 0x3f3f3f3f; const ll ll_INF = 233333333333333; const int maxn = 1e4+10; struct test { ll a, b; }num[10], temp[10]; ll res[maxn]; int kase; void solve( int start, int n) { if (start == n) { ll sum = 0; //_test; for ( int i = 0; i<n; ++i) { //求结果 //printf("%lld %lld\n", num[i].a, num[i].b); sum += (num[i].b * sum + num[i].a); } res[kase++] = sum; } for ( int i = start; i<n; ++i) { //求全排列 swap(num[start], num[i]); //交换 solve(start+1, n); swap(num[i], num[start]); //复位 } } int main( void ) { int t; scanf ( "%d" , &t); while (t--) { kase = 0; fill(res, res+maxn, ll_INF); //将储存答案的数组元素全部初始化为一个足够大的值,方便下面直接调用函数求最小值 int n; scanf ( "%d" , &n); for ( int i = 0; i<n; ++i) scanf ( "%lld%lld" , &num[i].a, &num[i].b); solve(0, n); //计算出所有排列的可能结果 printf ( "%lld\n" , *min_element(res, res+maxn)); } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步