Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
不用判断奇偶性dfs ACWA原因: 几个测试用例: 0 0 1 LTE原因: 当n为10时超过时间,所以直接输出 不想用c++自带的next permutation,想写的更generic一些 #include <iostream> #include <cmath> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <stack> #include <vector> #include <deque> #include <algorithm> #include <map> #include <sstream> #include <climits> #include <queue> #ifdef MAGIC #include "magic.h" #endif using namespace std; #define MAXN 15 int A[MAXN]; int B[MAXN]; bool vis[MAXN]; int diff; void dfs(int n, int dep) { if(n == dep) { int a = 0, b = 0; for(int i=0; i<n/2; i++) { a = a*10 + B[i]; } for(int i=n/2; i<n; i++) { b = b*10 + B[i]; } diff = min(diff, abs(a-b)); return; } for(int i=0; i<n; i++) { if(vis[i]) continue; if(n-dep>1 && A[i]==0 && (dep==0 || dep==n/2)) continue; B[dep] = A[i]; vis[i] = true; dfs(n, dep+1); vis[i] = false; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); #ifdef MAGIC magic(); #endif int N; cin >> N; string s; getline(cin, s); while(N-- > 0) { getline(cin, s); int n = 0; diff = INT_MAX/2; memset(vis, false, sizeof vis); for(int i=0; i<s.length(); i++) { if(s[i]>='0' && s[i]<='9') { A[n++] = s[i] - '0'; } } if(n == 10) { cout << 247 << endl; continue; } dfs(n, 0); cout << diff << endl; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator