JZOJsenior4908.【NOIP2016提高组复赛】愤怒的小鸟
来源:互联网 发布:数据库逻辑模型图 编辑:程序博客网 时间:2024/05/22 01:38
problem
Description
Input
Output
Sample Input
Sample Input1:
2
2 0
1.00 3.00
3.00 3.00
5 2
1.00 5.00
2.00 8.00
3.00 9.00
4.00 8.00
5.00 5.00
Sample Input2:
3
2 0
1.41 2.00
1.73 3.00
3 0
1.11 1.41
2.34 1.79
2.98 1.49
5 0
2.72 2.72
2.72 3.14
3.14 2.72
3.14 3.14
5.00 5.00
Sample Input3:
1
10 0
7.16 6.28
2.02 0.38
8.33 7.78
7.68 2.09
7.46 7.86
5.77 7.44
8.24 6.72
4.42 5.11
5.42 7.79
8.15 4.99
Sample Output
Sample Output1:
1
1
Sample Output2:
2
2
3
Sample Output3:
6
Data Constraint
analysis
容易注意到一个很明显很关键的东西:
保证1≤n≤18
这种小的离奇的数据范围提醒着我们——正解状压DP (打记忆化搜索暴力的我会告诉你2000ms的时间也能卡过去的)
其实
设
这还没完,还得预处理,预处理什么呢?
设
(因为穿过
对了不要忘记一只鸟打一只猪的情况,没考虑这个是要WA的
code
听说c++的精度问题很迷,所以注意精度!!!
时间复杂度
code1(1766ms)
#include<bits/stdc++.h>using namespace std;int sit[20][20],f[1<<18];double a[20][3];double x[20],y[20];int n,m,t,tot;bool judge(double x,double y){ return abs(x-y)<(1e-6); }int main(){ freopen("angrybirds.in","r",stdin); freopen("angrybirds.out","w",stdout); scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { scanf("%lf%lf",&x[i],&y[i]); } tot=0; memset(sit,0,sizeof(sit)); for (int i=1;i<=n;i++) { for (int j=i+1;j<=n;j++) { if (judge(x[i],x[j]))continue; double xx=(y[j]/x[j]-y[i]/x[i])/(x[j]-x[i]),yy=y[i]/x[i]-xx*x[i];//算抛物线方程 if (xx>=0)continue; int temp=0; for (int k=1;k<=n;k++) { if (judge(xx*x[k]+yy,y[k]/x[k])) { temp+=(1<<(k-1)); } } sit[i][j]=temp; } } memset(f,127,sizeof(f)); f[0]=0; for (int i=0;i<=(1<<n)-1;i++) { for (int j=1;j<=n;j++) { for (int k=j+1;k<=n;k++) { f[i|sit[j][k]]=min(f[i|sit[j][k]],f[i]+1); } f[i|(1<<(j-1))]=min(f[i|(1<<j-1)],f[i]+1);//一鸟打一猪 } } printf("%d\n",f[(1<<n)-1]); } return 0;}
code2(981ms)
可以看出上面那个code慢的原因就是,可能抛物线经过的猪有重复
比如打
判断状态是否重复,剪枝即可
时间复杂度
#include<bits/stdc++.h>using namespace std;int sit[20][20],f[1<<18];double a[20][3];double x[20],y[20];int n,m,t;bool judge(double x,double y){ return abs(x-y)<(1e-6); }int main(){ freopen("angrybirds.in","r",stdin); freopen("angrybirds.out","w",stdout); scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { scanf("%lf%lf",&x[i],&y[i]); } memset(sit,0,sizeof(sit)); for (int i=1;i<=n;i++) { for (int j=i+1;j<=n;j++) { if (judge(x[i],x[j]))continue; double xx=(y[j]/x[j]-y[i]/x[i])/(x[j]-x[i]),yy=y[i]/x[i]-xx*x[i]; if (xx>=0)continue; int temp=0; for (int k=1;k<=n;k++) { if (judge(xx*x[k]+yy,y[k]/x[k])) { temp+=(1<<(k-1)); } } sit[i][j]=temp; } } memset(f,127,sizeof(f)); f[0]=0; for (int i=0;i<=(1<<n)-1;i++) { for (int j=1;j<=n;j++) if(!(i&(1<<(j-1)))) { for (int k=j+1;k<=n;k++) { f[i|sit[j][k]]=min(f[i|sit[j][k]],f[i]+1); } f[i|(1<<(j-1))]=min(f[i|(1<<j-1)],f[i]+1); } } printf("%d\n",f[(1<<n)-1]); } return 0;}
code3(179ms)
真心不知道那些打到200ms-的爷都怎么打的,点开一看——
——发现只是加了一个break???
好吧我输了……
#include<bits/stdc++.h>using namespace std;int sit[20][20],f[1<<18];double a[20][3];double x[20],y[20];int n,m,t;bool judge(double x,double y){ return abs(x-y)<(1e-6); }int main(){ freopen("angrybirds.in","r",stdin); freopen("angrybirds.out","w",stdout); scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { scanf("%lf%lf",&x[i],&y[i]); } memset(sit,0,sizeof(sit)); for (int i=1;i<=n;i++) { for (int j=i+1;j<=n;j++) { if (judge(x[i],x[j]))continue; double xx=(y[j]/x[j]-y[i]/x[i])/(x[j]-x[i]),yy=y[i]/x[i]-xx*x[i]; if (xx>=0)continue; int temp=0; for (int k=1;k<=n;k++) { if (judge(xx*x[k]+yy,y[k]/x[k])) { temp+=(1<<(k-1)); } } sit[i][j]=temp; } } memset(f,127,sizeof(f)); f[0]=0; for (int i=0;i<=(1<<n)-1;i++) { for (int j=1;j<=n;j++) if(!(i&(1<<(j-1)))) { for (int k=j+1;k<=n;k++) { f[i|sit[j][k]]=min(f[i|sit[j][k]],f[i]+1); } f[i|(1<<(j-1))]=min(f[i|(1<<j-1)],f[i]+1); break; } } printf("%d\n",f[(1<<n)-1]); } return 0;}
- JZOJsenior4908.【NOIP2016提高组复赛】愤怒的小鸟
- 【NOIP2016提高组复赛】愤怒的小鸟
- 【NOIP2016提高组day2】愤怒的小鸟
- NOIP2016提高组day2 愤怒的小鸟
- 愤怒的小鸟(NOIP2016提高组Day2T3)
- [NOIP2016] 愤怒的小鸟
- NOIP2016愤怒的小鸟
- [NOIP2016]愤怒的小鸟
- 【NOIP2016】愤怒的小鸟
- NOIP2016 愤怒的小鸟
- 【noip2016提高组day2T3】【愤怒的小鸟】状压dp转移时的集合包含
- NOIP2016 T6 愤怒的小鸟
- [noip2016]愤怒的小鸟 题解
- 【jzoj4908】【NOIP2016提高组】【愤怒的小鸟】【状态压缩动态规划】
- NOIP2016提高组 第二天第三题 愤怒的小鸟angrybirds 题解
- C++ & Pascal——NOIP2016提高组day2 t3——愤怒的小鸟
- 【NOIP 2016 提高组】愤怒的小鸟
- NOIP2016提高组复赛
- java并发编程,线程池的使用
- WPS下搜狗输入法不能输入中文
- CodeBlocks主题及CodeBlocks13.12的安装及汉化
- PHP中使用cURL实现Get和Post请求的方法
- UVA —1368 DNA Consensus String
- JZOJsenior4908.【NOIP2016提高组复赛】愤怒的小鸟
- 51 node 1284 容斥原理
- 修改Django分页类,自定义实现分页功能
- WorkerMan学习篇:一
- 函数的基本概念
- CHECK the manual that corresponds TO your MySQL SERVER VERSION FOR the RIGHT syntax TO USE near.....
- 【双十一献礼】c语言数组、指针、与函数参数
- 欢迎使用CSDN-markdown编辑器
- 五大浏览器哪个最好用。。。