4429: [Nwerc2015] Elementary Math小学数学
Description
Ellen is teaching elementary math to her students and the time for the final exam has come. The exam consists of n questions. In each question the students have to add (+), subtract (−) or multiply (∗) a pair of numbers.
Ellen has already chosen the n pairs of numbers. All that remains is to decide for each pair which of the three possible operations the students should perform. To avoid students getting bored, Ellen wants to make sure that the n correct answers to her exam are all different.
Please help Ellen finish constructing the exam by automating this task.
Ellen给她的学生教小学数学。期末考试已经来临了。考试有n个题目,每一个题目学生们都要对一对数字进行加(+),减(-),乘(*)运算。
Ellen已经选好了n对数。剩下的是决定学生们应该对每对数执行什么运算。为了不让学生们感到厌烦,Ellen想确保n个正确答案都不一样。
请帮助Ellen自动化地构建考试。
Input
The input consists of:
• one line with one integer n (1≤n≤2500), the number of pairs of numbers;
• n lines each with two integers a and b (−10^6≤a,b≤10^6), a pair of numbers used.
输入包括:
第一行是一个整数n(1<=n<=2500),表示共有n道题目。
接下来n行每行有2个整数a和b(-10^6<=a,b<=10^6),表示每一题使用的整数。
Output
For each pair of numbers (a,b) in the same order as in the input, output a line containing a valid equation. Each equation should consist of five parts: a, one of the three operators, b, an equals sign (=), and the result of the expression. All the n expression results must be different.
If there are multiple valid answers you may output any of them. If there is no valid answer, output a single line with the string “impossible” instead.
对于输入中的每一对(a,b),输出一行有效的方程。每一个方程应该包含5部分:a,+、-、*中的一个运算符,b,=,答案。N个答案必须不同。
如果有多个有效答案,你可以输出任意一个。如果没有答案,输出“impossible”。
Sample Input
Sample input 1
4
1 5
3 3
4 5
-1 -6
Sample input 2
4
-4 2
-4 2
-4 2
-4 2
4
1 5
3 3
4 5
-1 -6
Sample input 2
4
-4 2
-4 2
-4 2
-4 2
Sample Output
Sample output 1
1 + 5 = 6
3 * 3 = 9
4 - 5 = -1
-1 - -6 = 5
Sample output 2
impossible
1 + 5 = 6
3 * 3 = 9
4 - 5 = -1
-1 - -6 = 5
Sample output 2
impossible
之前很naive的想学avl树最后debug不出只好先作罢……浪费了好长时间还被swanchD了ojz好久没搞题来道水题吧qwq
又是二分图匹配,左边的点是每一组数据a,b,右边的点是+-*得出的所有可能结果,统计一遍,匹配。
是道special judge……注意格式和long long
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<cmath> 7 #include<vector> 8 #include<algorithm> 9 #include<queue> 10 #include<map> 11 using namespace std; 12 #define Max 1e9 13 long long n,gs=0,dis=1e9,ans=0; 14 long long a[3000],b[3000],vis[100005],sv[3000][2]; 15 long long sum[100005],dx[3000],dy[100005],cx[3000],cy[100005]; 16 map<long long,int> pd; 17 int bfs() 18 { 19 queue<long long> q; 20 dis=Max; 21 memset(dx,0,sizeof(dx)); 22 memset(dy,0,sizeof(dy)); 23 for (int i=1;i<=n;i++) 24 { 25 if (!cx[i]) q.push(i); 26 } 27 while (!q.empty()) 28 { 29 long long x=q.front(); 30 q.pop(); 31 if (dx[x]>dis) break; 32 for (int i=1;i<=gs;i++) 33 { 34 if (!dy[i]&&(a[x]+b[x]==sum[i]||a[x]-b[x]==sum[i]||a[x]*b[x]==sum[i])) 35 { 36 dy[i]=dx[x]+1; 37 if (!cy[i]) dis=dy[i]; 38 else 39 { 40 dx[cy[i]]=dy[i]+1; 41 q.push(cy[i]); 42 } 43 } 44 } 45 } 46 return dis!=Max; 47 } 48 int find(long long x) 49 { 50 for (int i=1;i<=gs;i++) 51 { 52 if (!vis[i]&&(a[x]+b[x]==sum[i]||a[x]-b[x]==sum[i]||a[x]*b[x]==sum[i])&&dy[i]==dx[x]+1) 53 { 54 vis[i]=1; 55 if (cy[i]&&dy[i]==dis) continue; 56 if (!cy[i]||find(cy[i])) 57 { 58 cx[x]=i;cy[i]=x; 59 return 1; 60 } 61 } 62 } 63 return 0; 64 } 65 int main() 66 { 67 scanf("%lld",&n); 68 for (int i=1;i<=n;i++) 69 { 70 scanf("%lld%lld",&a[i],&b[i]); 71 long long x=a[i]+b[i]; 72 if (!pd[x]) 73 sum[++gs]=x,pd[x]=1; 74 x=a[i]-b[i]; 75 if (!pd[x]) 76 sum[++gs]=x,pd[x]=1; 77 x=a[i]*b[i]; 78 if (!pd[x]) 79 sum[++gs]=x,pd[x]=1; 80 } 81 while (bfs()) 82 { 83 memset(vis,0,sizeof(vis)); 84 for (int i=1;i<=n;i++) 85 { 86 if (!cx[i]) 87 { 88 ans+=find(i); 89 } 90 } 91 } 92 if (ans!=n) 93 {printf("impossible");return 0;} 94 else 95 { 96 for (int i=1;i<=n;i++) 97 { 98 if (a[i]+b[i]==sum[cx[i]]) 99 printf("%lld + %lld = %lld ",a[i],b[i],sum[cx[i]]); 100 else if (a[i]-b[i]==sum[cx[i]]) 101 printf("%lld - %lld = %lld ",a[i],b[i],sum[cx[i]]); 102 else if (a[i]*b[i]==sum[cx[i]]) 103 printf("%lld * %lld = %lld ",a[i],b[i],sum[cx[i]]); 104 } 105 } 106 }
本来是想找道水题来不让swanchD我的QAQ结果发现是道权限题orzorz这就很尴尬了……还是感谢某dalao的支持x
有谁告诉我avl树怎么写啊……