Exercises 1

Communication System // oj 1018

这道题如果纯暴力容易超时。考虑到在一定的bandwidth下,price越小越好,进行优化。

第一步改进是用multimap储存,对于每一层中的每一个设备,只要找不同层里bandwidth比它大的中 最小的price。这样已经可以过了,但800+ms

第二步改进,用map储存,在输入时,每层只保留bandwidth相同的里面price最小的。这样是700-800ms

第三步改进,不要再去搜“每一层中的每一个设备”。考虑到,我们想让最小的B尽量大,我们在输入时找到最大B最小的那一层,只遍历这一层的每一个设备即可。这样是10-20ms,在数据结构和排序上还可以再优化,比如找到b比它大的最小price。

问题在于,这道题卡了很久,原因有二:

①搜索的框架不是很清晰。上述第三步改进,最初自己搞成了用各层中bandwidth最大的那个设备与其他层遍历,这样有遗漏,因为对于非“最小之最大”所在层,如果层里有一些price很小的、bandwidth非最大的,这些会被遗漏。实际上,选取一层,对该层所有设备与其他层进行遍历,则减小了遗漏的可能,为了彻底杜绝遗漏,我们选取最大bandwidth是所有层中最小的那一层,即“最小之最大”所在层。

②多组数据下的初始化,这里包括了ans 和 m,编程习惯还是不太好呀……

#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int n;
typedef map<int,int>::iterator IT;
double ans;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		ans=0;
		map<int,int> m[102];
		int minRow,minB=(1<<30);
		for(int i=0;i<n;++i){
			int tt,b,p;
			scanf("%d",&tt);
			while(tt--){
				scanf("%d%d",&b,&p);
				IT it=m[i].find(b);
				if(it==m[i].end()) m[i].insert(make_pair(b,p));
				else if(it->second>p){
					m[i].erase(it);
					m[i].insert(make_pair(b,p));
				}
			}
			if((--m[i].end())->first<minB){
				minB=(--m[i].end())->first;
				minRow=i;
			}
		}
		IT t=m[minRow].begin();
		for(;t!=m[minRow].end();++t){
			int P=t->second;
			bool f=1;
			for(int j=0;j<n;++j){
				if(j==minRow) continue;
				IT tt=m[j].lower_bound(t->first);
				if(tt!=m[j].end()) {
					int minP=(1<<30);
					for(;tt!=m[j].end();++tt) minP=min(minP,tt->second);
					P+=minP;
				}
				else{
					f=0;break;
				} 	
			}
			if(f) ans=max(ans,(double)t->first/P);
		}
		printf("%.3lf\n",ans);
	}
}

附上自己搞的测试数据

3
4 100 25 100 25 150 35 80 25
3 120 80 155 40 120 100
3 100 110 100 100 100 100 【0.606】
3
2 150 1 180 35
3 120 100 80 80 60 10
3 100 110 80 90 150 70 【0.741】

选择客栈 // oj 4034

与其说是动规,不如说是递推。

这些数学方面的东西练习得太少。所以,这是个潜在隐患。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//初始思路是搜索,但数据量是200000,那怎么办?数学 
//前i家店方案总数,其实只要找到 i 之前的第一个价格合适的即可(不考虑颜色) 
int n,k,p;
struct hotel{
	int color;
	int pay;
}h[200002]; 
int dp[200002]={0}; //前i家店方案总数 
int a[200002]={0};//前i-1中与i同色调的个数 
int b[200002]={0};//前i-1中与i同色调的最大号
int ans[200002]={0}; //第i家店的方案数 

int main(){
	scanf("%d%d%d",&n,&k,&p);
	for(int i=1;i<=n;++i) {
		scanf("%d%d",&h[i].color,&h[i].pay);
		for(int j=i-1;j>0;--j){
			if(h[j].color==h[i].color){
				b[i]=j;
				a[i]=a[j]+1;
				break;
			}
		}
	}
	for(int i=2;i<=n;++i){
		int k; //前i中价格合适的最大号 
		for(k=i;k>0;--k) if(h[k].pay<=p) break; 
		if(k>=b[i]) ans[i]=a[i];
		else ans[i]=ans[b[i]]; //价格合适的最大号小于同色最大号 
		dp[i]=ans[i]+dp[i-1];
	}
	printf("%d",dp[n]);
}

恼人的青蛙  // oj 1054 2812

这道题本来可以没那么困难的,然而长时间没写程序,一些debug的感觉丢了不少

这道题的易错点在于,寻找路径时,如果下一个点找不到,说明这条路径就根本不通,因为如果通的话,按部就班找下去,一定能找到靠近边缘的最后一个点。

#include <iostream>
#include <algorithm>
using namespace std;
struct loc{
	int x,y;
	loc(){}
	loc(int x_,int y_):x(x_),y(y_){}
	
} tread[5002];
bool operator<(const loc&a,const loc&b){
	if(a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}
int R,C,N;
int maxStep,cntStep;

int main(){
	scanf("%d%d%d",&R,&C,&N);
	for(int i=0;i<N;++i) scanf("%d%d",&tread[i].x,&tread[i].y);	
	sort(tread,tread+N);
	maxStep=2;
	for(int i=0;i<N-2;++i){
		for(int j=i+1;j<N-1;++j){
			int dx=tread[j].x-tread[i].x;
			int dy=tread[j].y-tread[i].y;
			int px=tread[i].x-dx, py=tread[i].y-dy;
			if(px>0&&px<=R&&py>0&&py<C) continue; //确保这是起始点
			px=tread[i].x+(maxStep-1)*dx;
			py=tread[i].y+(maxStep-1)*dy;
			if(px>R||px<1) break;
			if(py>C||py<1) continue; //y越界,换下一个j 
			cntStep=2;
			int x=tread[j].x+dx, y=tread[j].y+dy;
			while(x>0&&y>0&&x<=R&&y<=C){ 
				if(!binary_search(tread,tread+N,loc(x,y))){
					cntStep=0; //坑死我了…… 
					break;
				}
				x+=dx;
				y+=dy;
				++cntStep;
			}
			if(cntStep>maxStep) maxStep=cntStep;
		}
	}
	if(maxStep<=2) maxStep=0;
	printf("%d\n",maxStep);		
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Focusing on high-dimensional applications, this 4th edition presents the tools and concepts used in multivariate data analysis in a style that is also accessible for non-mathematicians and practitioners. It surveys the basic principles and emphasizes both exploratory and inferential statistics; a new chapter on Variable Selection (Lasso, SCAD and Elastic Net) has also been added. All chapters include practical exercises that highlight applications in different multivariate data analysis fields: in quantitative financial studies, where the joint dynamics of assets are observed; in medicine, where recorded observations of subjects in different locations form the basis for reliable diagnoses and medication; and in quantitative marketing, where consumers’ preferences are collected in order to construct models of consumer behavior. All of these examples involve high to ultra-high dimensions and represent a number of major fields in big data analysis. The fourth edition of this book on Applied Multivariate Statistical Analysis offers the following new features: A new chapter on Variable Selection (Lasso, SCAD and Elastic Net) All exercises are supplemented by R and MATLAB code that can be found on www.quantlet.de. The practical exercises include solutions that can be found in Härdle, W. and Hlavka, Z., Multivariate Statistics: Exercises and Solutions. Springer Verlag, Heidelberg. Table of Contents Part I Descriptive Techniques Chapter 1 Comparison of Batches Part II Multivariate Random Variables Chapter 2 A Short Excursion into Matrix Algebra Chapter 3 Moving to Higher Dimensions Chapter 4 Multivariate Distributions Chapter 5 Theory of the Multinormal Chapter 6 Theory of Estimation Chapter 7 Hypothesis Testing Part III Multivariate Techniques Chapter 8 Regression Models Chapter 9 Variable Selection Chapter 10 Decomposition of Data Matrices by Factors Chapter 11 Principal Components Analysis Chapter 12 Factor Analysis Chapter 13 Cluster Analysis Chapter 14 Discri
### 回答1: 100 numpy exercises是一份以Python库NumPy为主题的练习题集合。NumPy是Python中最常用的数值计算库之一,它提供了高效的数组操作和向量化计算功能。这份练习题集的目的是帮助学习者更好地理解和掌握NumPy的各种功能和用法。 这份题集包含了100个不同的练习题,分为易、中、难三个难度级别。每个练习题都有明确的目标,例如创建特定的数组、对数组进行操作和计算,以及使用NumPy库中的各种函数和方法等。题集中的每个练习都带有详细的题目描述和示例代码,学习者可以通过阅读题目描述和参考示例代码来理解问题的背景和解决思路。 通过完成这份练习题集,学习者可以提升自己在NumPy库的应用能力,深入了解NumPy的各种功能和用法。同时,通过实践解决不同难度的问题,学习者还可以提高自己的编程能力和解决问题的思维能力。 对于想要学习NumPy库的人来说,这份练习题集是一份很好的学习资源。学习者可以逐个尝试解决每个练习题,并在解题过程中不断探索和学习NumPy库的各种功能和技巧。完成这份练习题集后,学习者将会对NumPy库有更深入的理解,并能够灵活应用NumPy库解决实际问题。 总之,100 numpy exercises提供了一系列以NumPy库为主题的练习题,帮助学习者提升自己在NumPy库的应用能力,深入理解NumPy库的各种功能和用法,同时也能提高编程和问题解决能力。这份练习题集对于想要学习NumPy的人来说,是一份很好的学习资源。 ### 回答2: 《100 NumPy练习题》(中文版)是一本关于NumPy库的练习题集。NumPy是一个功能强大的开源数学库,它提供了高性能的多维数组对象和用于处理这些数组的工具。 这本练习题集共包含100个通过NumPy库实现的练习题,旨在帮助读者提升对NumPy的理解和应用能力。这些练习题涵盖了NumPy库的各个方面,包括数组的创建、索引和切片、数组的运算和处理、数组的变形和操作等内容。 这本练习题集的特点如下: 1. 简洁明了:每个练习题都包含了问题描述和具体要求,读者可以根据需求在代码中填充相应的代码,然后运行并查看结果。 2. 渐进难度:练习题的难度是逐渐增加的,从简单的问题逐步过渡到复杂的问题,帮助读者逐步提升技能。 3. 具体实例:练习题中提供了许多具体实例,通过实际的应用场景帮助读者理解NumPy库的用法。 4. 答案解析:每个练习题附有详细的答案解析,读者可以通过对比自己的答案来检查和理解问题的解决思路。 通过完成这些练习题,读者可以巩固NumPy的基础知识,并掌握更高级和复杂的用法。这对于希望深入学习数据分析、科学计算和机器学习领域的读者来说,是一本很好的参考书。 总之,《100 NumPy练习题》(中文版)是一本适合各个层次的读者学习NumPy库的练习题集,它将帮助读者提升对NumPy库的理解和应用能力,是学习NumPy的一本很好的参考书籍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值