【bzoj4429】[Nwerc2015] Elementary Math小学数学
来源:互联网 发布:whatsapp for mac 编辑:程序博客网 时间:2024/06/11 02:18
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
Sample Output
Sample output 1
1 + 5 = 6
3 * 3 = 9
4 - 5 = -1
-1 - -6 = 5
Sample output 2
impossible
题解
将算式与答案连边,直接跑匈牙利即可
代码
#include<bits/stdc++.h>#define ll long long#define N 100005using namespace std;inline int read(){ int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int c[10005][5],flag[10005],match1[10005],match2[10005];struct node{ll v;int id;}ls[10005];ll Hash[10005];int T,n,a[10005],b[10005],ans,cnt,num;bool cmp(node a,node b){return a.v<b.v;}bool find(int u){ for (int i=1;i<=c[u][0];i++) { int v=c[u][i]; if (flag[v]==T) continue; flag[v]=T; if (match1[v]==-1||find(match1[v])) { match2[u]=v; match1[v]=u; return 1; } } return 0;}int main(){ n=read(); for (int i=1;i<=n;i++) { a[i]=read();b[i]=read(); ls[++cnt].v=a[i]+b[i];ls[cnt].id=i; ls[++cnt].v=a[i]-b[i];ls[cnt].id=i; ls[++cnt].v=(ll)a[i]*b[i];ls[cnt].id=i; } sort(ls+1,ls+cnt+1,cmp); ls[0].v=-10000000000000; for (int i=1;i<=cnt;i++) { if (ls[i].v!=ls[i-1].v) num++; c[ls[i].id][0]++; c[ls[i].id][c[ls[i].id][0]]=num; Hash[num]=ls[i].v; } memset(match1,-1,sizeof(match1)); for (int i=1;i<=n;i++) { T=i; if (find(i)) ans++; } if (ans!=n) puts("impossible"); else { for (int i=1;i<=n;i++) { ll x=Hash[match2[i]]; if (a[i]+b[i]==x) printf("%d + %d = %lld\n",a[i],b[i],x); else if ((ll)a[i]*b[i]==x) printf("%d * %d = %lld\n",a[i],b[i],x); else printf("%d - %d = %lld\n",a[i],b[i],x); } } return 0;}
- 【NWERC2015】【BZOJ4429】Elementary Math小学数学
- bzoj4429: [Nwerc2015] Elementary Math小学数学
- 【bzoj4429】[Nwerc2015] Elementary Math小学数学
- 【BZOJ4429】[Nwerc2015] Elementary Math小学数学【二分图匹配】
- BZOJ4429(Nwerc2015)[Elementary Math小学数学]--离散+二分图最大匹配
- BZOJ 4429: [Nwerc2015] Elementary Math小学数学
- BZOJ4429 Elementary Math小学数学 (二分图匹配)
- bzoj 4429: [Nwerc2015] Elementary Math小学数学 网络流
- BZOJ 4429 [Nwerc2015] Elementary Math小学数学 二分图匹配
- NWERC2015 Elementary Math
- NWERC2015-Elementary Math
- Elementary Math、
- UVALive 7427Elementary Math
- Elementary Math Gym
- 小学数学
- 小学数学
- 小学数学
- 小学数学
- Mysql 热备份-读写分离
- eslint代码规范,规则配置
- CentOS7上安装RabbitMQ与win互通测试
- pymssql 安装和使用
- Python 发送email的方法
- 【bzoj4429】[Nwerc2015] Elementary Math小学数学
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A 题目忘了
- CART树回归
- STM32平台移植uCOSIII
- Selenium IDE 使用方法小结
- POJ 1751 Highways (kruskal)
- Selenium-如何截取图像
- threejs学习随记(一)
- 开发中sql server一些问题的解决办法