某地区发生了地震,灾区已经非常困难,灾民急需一些帐篷、衣物、食品和血浆等物资。可通往灾区的道路到处都是塌方,70%以上的路面损坏,桥梁全部被毁。国家立即启动应急预案,展开史上最大强度非作战空运行动,准备向灾区空投急需物资。
一方有难,八方支援。现在已知有N个地方分别有A1,A2,….,An个物资可供调配。目前灾区需要物资数量为M。
现在,请你帮忙算一算,总共有多少种物质调度方案。
假设某地方一旦被选择调配,则其物资数全部运走。
第一行: K 表示有多少组测试数据。
接下来对每组测试数据有2行,第1行: N M
第2行:A1 A2 …… An
2≤K≤8 1<N≤100 1<M≤1000 1≤ Ai≤1000
所有数据都是正整数。输入数据之间有一个空格。
假设给定的数据至少有一种调度方案。
对于每组测试数据,输出一行:物资调度的总方案数
2
4 4
1 1 2 2
4 6
1 1 2 2
3
1
第七届河南省赛
思路: DFS 搜索
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #define INF 0x3f3f3f3f #define eps 1e-8 #define Si(a) scanf("%d", &a) #define Sl(a) scanf("%lld", &a) #define Sf(a) scanf("%lf", &a) #define Ss(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while((a)--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define LL long long #define PI acos(-1.0) using namespace std; int n, m; int ans; int vis[150]; int a[150]; void dfs(int s, int sum) { if(sum==m) { ans++; return ; } if(sum > m) return ; for(int i = s; i < n; i++) { if(sum+a[i] <= m && !vis[i]) { vis[i] = 1; dfs(i+1, sum+a[i]); vis[i] = 0; } } } int main() { int t;Si(t); W(t) { CLR(vis, 0); CLR(a, 0); scanf("%d%d", &n,&m); for(int i = 0; i < n; i++) Si(a[i]); ans = 0; dfs(0, 0); Pi(ans); } return 0; } /************************************************************** Problem: 10401 User: l1994 Language: C++ Result: Accepted Time:0 ms Memory:1560 kb ****************************************************************/
10 8
1 2 5
1 3 2
2 3 11
2 4 6
2 4 4
6 7 10
6 10 5
10 7 2
5
2 3
1 4
3 7
6 7
8 3
5
5
-1
5
-1
第七届河南省赛
思路: 最短路 dijkstra的变形,模板改一点就行了
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #define INF 0x3f3f3f3f #define eps 1e-8 #define Si(a) scanf("%d", &a) #define Sl(a) scanf("%lld", &a) #define Sf(a) scanf("%lf", &a) #define Ss(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while((a)--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define LL long long #define PI acos(-1.0) using namespace std; int n, m; int map[200][200]; int vis[200]; int dis[200]; void init() { for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n; ++j) map[i][j] = i==j ? 0 : INF; } } void dij(int s,int t) { CLR(vis, 0); int i, j, k; for(i = 1; i <= n; ++i) dis[i] = map[s][i]; vis[s] = 1; for(i = 1; i < n; ++i) { int minn = INF; k = s; for(j = 1; j <= n; ++j) { if(!vis[j] && minn > dis[j]) { minn = dis[j]; k = j; } } vis[k] = 1; for(j = 1; j <= n; ++j) { if(!vis[j]) dis[j] = min(dis[j], max(dis[k],map[k][j])); } } if(dis[t] != INF) Pi(dis[t]); else Pi(-1); } int main() { while(scanf("%d%d", &n,&m)!=EOF) { init(); int i, j, k; int s, t, v; for(i = 0; i < m; ++i) { scanf("%d%d%d", &s, &t, &v); if(map[s][t] > v) map[s][t] = map[t][s] = v; } int q; Si(q); W(q) { scanf("%d%d", &s, &t); dij(s, t); } } return 0; } /************************************************************** Problem: 10400 User: l1994 Language: C++ Result: Accepted Time:4 ms Memory:1720 kb ****************************************************************/
Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远。由于受软硬件设计所限,机器人卡尔只能定点跳远。若机器人站在(X,Y)位置,它可以原地蹦,但只可以在(X,Y),(X,-Y),(-X,Y),(-X,-Y),(Y,X),(Y,-X),(-Y,X),(-Y,-X)八个点跳来跳去。
现在,Dr. Kong想在机器人卡尔身上设计一个计数器,记录它蹦蹦跳跳的数字变化(S,T),即,路过的位置坐标值之和。
你能帮助Dr. Kong判断机器人能否蹦蹦跳跳,拼出数字(S,T)吗?
假设机器人卡尔初始站在(0,0)位置上。
第一行: K 表示有多少组测试数据。
接下来有K行,每行:X Y S T
1≤K≤10000 -2*109 <= X , Y, S, T <= 2*109
数据之间有一个空格。
对于每组测试数据,输出一行:Y或者为N,分别表示可以拼出来,不能拼出来
3
2 1 3 3
1 1 0 1
1 0 -2 3
Y
N
Y
第七届河南省赛
思路: 扩展欧几里得。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #define INF 0x3f3f3f3f #define eps 1e-8 #define Si(a) scanf("%d", &a) #define Sl(a) scanf("%lld", &a) #define Sf(a) scanf("%lf", &a) #define Ss(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while((a)--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define LL long long #define PI acos(-1.0) using namespace std; LL exgcd(LL a, LL b, LL &x, LL &y) { if(b==0) { x = 1; y = 0; return a; } LL ans = exgcd(b, a%b, x, y); LL temp = x; x = y; y = temp - a/b*y; return ans; } int main() { int k;Si(k); W(k) { LL x, y, s, t; scanf("%lld%lld%lld%lld", &x, &y, &s, &t); LL a, b,c, d, g; g = exgcd(x, y, a, b); c = a*t/g; d = b*t/g; a *= s/g; b *= s/g; if(s%g==0 && t%g==0) { /*a = a*(s/g); b = b*(s/g); c = c*(t/g); d = d*(t/g);*/ bool flag = 0; for(int i = -2; i <= 2; ++i) { LL x1, y1; x1 = a+x/g*i; y1 = b-y/g*i; for(int j = -2; j <= 2; ++j) { LL x2, y2; x2 = c+x/g*j; y2 = d-y/g*j; if((x1+y2)%2 == 0 && (x2+y1)%2 == 0) { flag = 1;break; } } } if(flag) puts("Y"); else puts("N"); } else puts("N"); } return 0; } /************************************************************** Problem: 10402 User: l1994 Language: C++ Result: Accepted Time:32 ms Memory:1560 kb ****************************************************************/
某山区的孩子们上学必须经过一条凹凸不平的土路,每当下雨天,孩子们非常艰难。现在村里走出来的Dr. Kong决定募捐资金重新修建着条路。由于资金有限,为了降低成本,对修好后的路面高度只能做到单调上升或单调下降。
为了便于修路,我们将整个土路分成了N段,每段路面的高度分别A1,A2,….,An。由于将每一段路垫高或挖低一个单位的花费成本相同,修路的总费用与路面的高低成正比。
现在Dr. Kong希望找到一个恰好含N个元素的不上升或不下降序列B1,B2,….,Bn,作为修过的路路段的高度。要求:
| A1-B1| + | A2–B2| + ... + | An-Bn|------>最小
第一行: K 表示有多少组测试数据。
接下来对每组测试数据:
第1行: N 表示整个土路分成了N段
第2~N+1行: A1 A2 ……AN 表示每段路面的高度
2≤k≤10 0≤Ai≤107 0≤N≤500 (i=1,…, N)
所有数据都是整数。 数据之间有一个空格。
数据保证| A1-B1|+| A2-B2|+ ... +| An-Bn|的最小值不会超过109
对于每组测试数据,输出占一行:| A1-B1|+| A2-B2|+ ... +| An-Bn|的最小值。
2
7
1 3 2 4 5 3 9
5
8 6 5 6 2
3
1
第七届河南省赛
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #define INF 0x3f3f3f3f #define eps 1e-8 #define Si(a) scanf("%d", &a) #define Sl(a) scanf("%lld", &a) #define Sf(a) scanf("%lf", &a) #define Ss(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while((a)--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define LL long long #define PI acos(-1.0) using namespace std; const int N = 520; int a[N], b[N], dp[N][N]; int main() { int T; Si(T); W(T) { CLR(a, 0); CLR(b, 0); CLR(dp, 0); int n; Si(n); int i, j, k; for(i = 0; i < n; ++i) { Si(a[i]); b[i] = a[i]; } sort(b, b+n); for(i = 0; i < n; ++i) { int m = INF; for(j = 0; j < n; ++j) { for(k = 0; k <= j; ++k) { m = min(m, dp[i][k]); } dp[i+1][j] = m+abs(b[j] - a[i]); } } int m1 = INF; for(i = 0; i < n; ++i) m1 = min(m1, dp[n][i]); reverse(a, a+n); for(i = 0; i < n; ++i) { int m = INF; for(j = 0; j < n; ++j) { for(k = 0; k <= j; ++k) { m = min(m, dp[i][k]); } dp[i+1][j] = m+abs(b[j] - a[i]); } } int m2 = INF; for(i = 0; i < n; ++i) m2 = min(m2, dp[n][i]); Pi(min(m1, m2)); } return 0; } /************************************************************** Problem: 10403 User: l1994 Language: C++ Result: Accepted Time:1320 ms Memory:2624 kb ****************************************************************/
73+42=16
5+8=13
0001000+000200=00030
0+0=0
TRUE
FALSE
TRUE
第七届河南省赛
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #define INF 0x3f3f3f3f #define eps 1e-8 #define Si(a) scanf("%d", &a) #define Sl(a) scanf("%lld", &a) #define Sf(a) scanf("%lf", &a) #define Ss(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while((a)--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define LL long long #define PI acos(-1.0) using namespace std; int main() { char s[100]; char a[3][9]; while(gets(s)) { if(strcmp(s, "0+0=0")==0) break; int ls = strlen(s); int i, j, k, t; int la, lb, lc; la = lb = lc = 0; CLR(a, 0); for(i = 0; ; i++) { if(s[i]=='+') break; a[0][la++] = s[i]-'0'; } for(j = i+1; ; j++) { if(s[j] == '=' ) break; a[1][lb++] = s[j]-'0'; } for(k = j+1; k <ls; k++) { a[2][lc++] = s[k]-'0'; } int x = 0, y = 0, z = 0; for(t = la-1; t >= 0; t--) x = x*10+a[0][t]; for(t = lb-1; t >= 0; t--) y = y*10+a[1][t]; for(t = lc-1; t >= 0; t--) z = z*10+a[2][t]; if( x+y == z ) puts("TRUE"); else puts("FALSE"); } return 0; } /************************************************************** Problem: 10399 User: l1994 Language: C++ Result: Accepted Time:0 ms Memory:1560 kb ****************************************************************/
Given N (4 <= N <= 100) rectangles and the lengths of their sides ( integers in the range 1..1,000), write a program that finds the maximum K for which there is a sequence of K of the given rectangles that can "nest", (i.e., some sequence P1, P2, ..., Pk, such that P1 can completely fit into P2, P2 can completely fit into P3, etc.).
A rectangle fits inside another rectangle if one of its sides is strictly smaller than the other rectangle's and the remaining side is no larger. If two rectangles are identical they are considered not to fit into each other. For example, a 2*1 rectangle fits in a 2*2 rectangle, but not in another 2*1 rectangle.
The list can be created from rectangles in any order and in either orientation.
The first line of input gives a single integer, 1 ≤ T ≤10, the number of test cases. Then follow, for each test case:
* Line 1: a integer N , Given the number ofrectangles N<=100
* Lines 2..N+1: Each line contains two space-separated integers X Y, the sides of the respective rectangle. 1<= X , Y<=5000
Output for each test case , a single line with a integer K , the length of the longest sequence of fitting rectangles.
1
4
8 14
16 28
29 12
14 8
2
第七届河南省赛
思路: LIS
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #define INF 0x3f3f3f3f #define eps 1e-8 #define Si(a) scanf("%d", &a) #define Sl(a) scanf("%lld", &a) #define Sf(a) scanf("%lf", &a) #define Ss(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while((a)--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define LL long long #define PI acos(-1.0) using namespace std; int dp[1010]; struct node{ int r, c; }p[10100]; bool cmp(node a, node b) { if(a.r != b.r) return a.r < b.r; return a.c < b.c; } int main() { int t; Si(t); W(t) { int n; Si(n); int i, j; int r, c; for(i = 0; i < n; ++i) { scanf("%d%d", &r, &c); dp[i] = 1; if(r > c) { p[i].r = r; p[i].c = c; } else { p[i].c = r; p[i].r = c; } } sort(p, p+n, cmp); for(i = 0; i < n; ++i) { for(j = 0; j < i; ++j) { if(p[i].r == p[j].r && p[i].c == p[j].c) continue; if(dp[i] < dp[j]+1 && p[i].r >= p[j].r && p[i].c >= p[j].c) { dp[i] = dp[j]+1; } } } sort(dp, dp+n); Pi(dp[n-1]); } return 0; } /************************************************************** Problem: 10396 User: l1994 Language: C++ Result: Accepted Time:0 ms Memory:1648 kb ****************************************************************/