当前位置: 首页 > wzjs >正文

dw里响应式网站怎么做新闻发布会

dw里响应式网站怎么做,新闻发布会,网站建设备案不通过,天猫网站建设可行性分析目录 1.P2085 最小函数值 题目 分析 方法1:暴力求解 方法2:二次函数的性质(推荐!) 代码 提交结果 2.P1631 序列合并 分析 方法1:建两个堆 第一版代码 提交结果 第二版代码 提交结果 第三版代码 提交结果 方法2:只建一个堆 代码 提交结果 1.P2085 最小函数值…

目录

1.P2085 最小函数值 

题目

分析

方法1:暴力求解

方法2:二次函数的性质(推荐!)

代码

提交结果

2.P1631 序列合并

分析

方法1:建两个堆

第一版代码

提交结果

第二版代码

提交结果

第三版代码

提交结果

方法2:只建一个堆

代码

提交结果


1.P2085 最小函数值 

题目

https://www.luogu.com.cn/problem/P2085

分析

从题目的"输出将这 n 个函数所有可以生成的函数值排序后的前 m 个元素"可以看出是堆排序的TopK问题

方法1:暴力求解

求出所有函数中的m个值,然后取前m个,n个函数各有m个值,时间复杂度过高,为O(m\cdot n),不建议使用

方法2:二次函数的性质(推荐!)

以测试用例分析画表:

会发现:x==4及之后x的值,均是x^2+7x+1最小,只有它的函数值需要入队

原因:x^2+7x+1的最高次的系数为1,阶位最高比4x^2+5x+33x^2+4x+5都要小,同是二次函数,二次项前的系数决定阶位的高低,因此有:

\lim_{x \to+ \infty} {4x^2+5x+3}>\lim_{x \to+ \infty} {3x^2+4x+5}>\lim_{x \to+ \infty} {x^2+7x+1}

从图像上也可以看出:

算法:首先将x==1的所有函数值计算出来,然后入堆,然后每次拿出最小的那个函数值来打印,把对应的之后删除堆顶元素,把对应的下一个函数值f(x+1)入堆

因为每次拿出最小的那个函数值,所以建立小根堆

因为要将对应的下一个函数值入堆,所以需要知道是哪个函数算的,因此要记录函数的编号,要算f(x+1),需要知道x,因此入堆的元素是结构体,建小根堆需要运算符重载

struct node
{ull f_x;//f(x)函数值 ull num;//函数的编号 ull x;bool operator<(const node& _node) const{return f_x>_node.f_x;//建立小根堆 }		
};

 例如

设f(x)=4x^2+5x+3,编号为1;g(x)=3x^2+4x+5,编号为2;h(x)=x^2+7x+1,编号为3;先将{12,1,1}、{12,2,1}、{9,3,1}入堆,发现函数值9最小,因此删除堆顶元素后将{h(1+1),3,2}入堆,以此类推......

注意:删除堆顶元素只能进行m次

代码

#include <iostream>
#include <queue>
#define endl "\n"  
using namespace std;
typedef unsigned long long ull;
const int N=1e5+10;
struct node
{ull f_x;//f(x)函数值 ull num;//函数的编号 ull x;bool operator<(const node& _node) const{return f_x>_node.f_x;//建立小根堆 }		
};
priority_queue<node> q;
ull A[N],B[N],C[N]; 
int main()
{ios::sync_with_stdio(false);cin.tie(0);ull n,m;cin>>n>>m;for (ull i=1;i<=n;i++)cin>>A[i]>>B[i]>>C[i];for (ull j=1;j<=n;j++)q.push({A[j]+B[j]+C[j],j,1});//x=1的所有函数值入队while(m--) {node tmp=q.top();cout<<q.top().f_x<<" ";q.pop();q.push({A[tmp.num]*(tmp.x+1)*(tmp.x+1)+B[tmp.num]*(tmp.x+1)+C[tmp.num],tmp.num,tmp.x+1});} return 0;
}

提交结果

2.P1631 序列合并

fhttps://www.luogu.com.cn/problem/P1631

分析

线索"从小到大表示这 N 个最小的和",显然属于TopK问题

方法1:建两个堆

由于是从小到大,应该建大根堆,将a[i]+b[j]的结果存入其中,保持大根堆内含N个元素,如果直接打印其堆顶元素的话结果是从大到小的,可以再建一个小根堆,将大根堆的堆顶元素存入小根堆,最后再打印大根堆的堆顶元素

第一版代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
const int N2=1e3+10;
typedef unsigned long long ull;
ull n,a[N1],b[N1],c[N2],d[N1],s;
priority_queue<ull,vector<ull>,less<ull>> tmp_heap;
priority_queue<ull,vector<ull>,greater<ull>> result_heap;
int main()
{cin>>n;for (int i=0;i<n;i++){cin>>a[i];}for (int i=0;i<n;i++){cin>>b[i];}for (int i=0;i<n;i++){for (int j=0;j<n;j++){c[s]=a[i]+b[j];if (tmp_heap.size()<n)		{tmp_heap.push(c[s]);	} else if (tmp_heap.top()>c[s]){tmp_heap.pop();tmp_heap.push(c[s]);}s++;}}	while (tmp_heap.size()){result_heap.push(tmp_heap.top());tmp_heap.pop();		} while(result_heap.size()){cout<<result_heap.top()<<" ";result_heap.pop();}return 0;
}
提交结果

超出内存的限制,其实不用创建c[N]数组,直接使用临时变量存储a[i]+b[j]的结果,节约空间

第二版代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
const int N2=1e3+10;
typedef unsigned long long ull;
ull n,a[N1],b[N1],d[N1],c;
priority_queue<ull,vector<ull>,less<ull>> tmp_heap;
priority_queue<ull,vector<ull>,greater<ull>> result_heap;
int main()
{cin>>n;for (int i=0;i<n;i++){cin>>a[i];}for (int i=0;i<n;i++){cin>>b[i];}for (int i=0;i<n;i++){for (int j=0;j<n;j++){c=a[i]+b[j];if (tmp_heap.size()<n)		{tmp_heap.push(c);	} else if (tmp_heap.top()>c){tmp_heap.pop();tmp_heap.push(c);}}}	while (tmp_heap.size()){result_heap.push(tmp_heap.top());tmp_heap.pop();		} while(result_heap.size()){cout<<result_heap.top()<<" ";result_heap.pop();}return 0;
}
提交结果

解决方法:有超时,说明for循环的次数过多,注意到题目的"单调不降序列",应尽量减少循环次数,观察发现,设外循环为变量i,内循环为变量j,当i不变时,随着j的增大a[i]+b[j]的值也在增大或者不变,当a[i]+b[j]在增大时,必定存在某个状态有tmp_heap.top()<c,不用入堆,应该立刻退出内循环,减少循环次数 

第三版代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
const int N2=1e3+10;
typedef unsigned long long ull;
ull n,a[N1],b[N1],d[N1],c;
priority_queue<ull,vector<ull>,less<ull>> tmp_heap;
priority_queue<ull,vector<ull>,greater<ull>> result_heap;
int main()
{cin>>n;for (int i=0;i<n;i++){cin>>a[i];}for (int i=0;i<n;i++){cin>>b[i];}for (int i=0;i<n;i++){for (int j=0;j<n;j++){c=a[i]+b[j];if (tmp_heap.size()<n)		{tmp_heap.push(c);	} else if (tmp_heap.top()>c){tmp_heap.pop();tmp_heap.push(c);}if (tmp_heap.top()<c)break;		}}	while (tmp_heap.size()){result_heap.push(tmp_heap.top());tmp_heap.pop();		} while(result_heap.size()){cout<<result_heap.top()<<" ";result_heap.pop();}return 0;
}
提交结果

方法2:只建一个堆

只建小根堆,由题目的"单调不降序列"

先将a[i]+b[0]入堆(堆中元素正好N个),之后每次先出堆顶元素,再入剩下的元素(a[i]+b[1]、a[i]+b[2]、...、a[i]+b[n]),这样每次出堆的元素都是当前堆中最小的,执行N次出堆即可,时间复杂度为O(NlogN)

以题目的测试用例为例

2+1=3 6+1=7 6+1=7

2+4=6 6+4=10 6+4=10

2+8=10 6+8=14 6+8=14

一行一行看,会发现:

a[1] + b[1] ≤ a[1] + b[2] ≤ a[1] + b[3] ≤ ... ≤ a[1] + b[n]
a[2] + b[1] ≤ a[2] + b[2] ≤ a[2] + b[3] ≤ ... ≤ a[2] + b[n]
a[3] + b[1] ≤ a[3] + b[2] ≤ a[3] + b[3] ≤ ... ≤ a[3] + b[n]
......

先将a[i]+b[1]入堆(因为是每一行不等式中最小的那个),之后取堆顶元素后再入堆,执行N次

能只将和入堆吗?如果不用两重循环的话

答:不能,只将和入堆会找不到下一个入堆的元素,入堆的应该是结构体

代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
int n,a[N1],b[N1];
struct node
{int i;int j;int sum;bool operator<(const node& x)const{return sum>x.sum;	}	
};priority_queue<node> heap; 
int main() 
{cin>>n;for (int i=0;i<n;i++)cin>>a[i];for (int i=0;i<n;i++)cin>>b[i];//前n个入堆for (int i=0;i<n;i++){heap.push({i,0,a[i]+b[0]}); }	//剩下的入堆for (int k=0;k<n;k++){node tmp=heap.top();heap.pop();		cout<<tmp.sum<<" ";if (tmp.j+1<n) {heap.push({tmp.i,tmp.j+1,a[tmp.i]+b[tmp.j+1]}); }} return 0;
}

提交结果

http://www.dtcms.com/wzjs/400964.html

相关文章:

  • 电话销售做网站推销中国新闻社
  • 企业网站通常包含的栏目哈尔滨seo
  • 网站建设费属于宣传费百度产品推广
  • 做网站的设计公司快速整站优化
  • 济南百度公司做网站吗福建网站建设制作
  • 怎么做一个链接网站线上培训机构
  • 江苏天目建设网站微信搜索seo优化
  • 济南正规网站建设公司哪家好北京seo方法
  • 长春手机网站腾讯朋友圈广告投放价格
  • 深圳做自适应网站成功营销案例100例
  • 网站制作难吗营销战略
  • 石嘴山网站定制开发建设sem是什么设备
  • 旅游做的视频网站企业员工培训课程有哪些
  • 网站开发后台框架怎么投放网络广告
  • 外贸网站建设 评价网站优化排名软件哪些最好
  • 专注律师微网站建设与律师微信营销seo是指搜索引擎营销
  • 做网站系统的域名被墙查询检测
  • deals网站建设搜索引擎优化的方法有哪些?
  • 小米品牌vi设计网站页面优化包括
  • 汕头企业网站推广方法企业营销策划及推广
  • 县政府网站加强制度建设webview播放视频
  • 公司的服务器能建设网站吗今日热搜榜排行榜
  • 大型集团网站seo外包网站
  • seo站长工具平台关键词优化快速
  • 品牌建设公司网站seo推广培训学费
  • 阿里巴巴网站装修宁波seo营销平台
  • java程序员做自己的网站老王搜索引擎入口
  • 如何套用别人网站做页面网站免费制作
  • 怎么做引流网站seo网站诊断方案
  • 开个网络公司需要多少钱广州seo搜索