当前位置:网站首页>1.21 learning summary
1.21 learning summary
2022-06-26 04:35:00 【After all, I still walk alone】
Today is also to make up the problem . Then what I want to review is that I have done a problem about greedy algorithm . At that time, this problem was not worked out for a long time . I haven't understood the topic . So I want to review again .
The title is as follows .

When I was doing this problem , Just a confused face . Mainly because I didn't understand , This is a line . It is equivalent to marking a one-dimensional array , Where can I go , The most incomprehensible thing at that time was that k. Only later , So that can . It's a counter . From the beginning to n. The title is not clear , I didn't understand it at that time . Then understand the meaning of the topic , This topic is equivalent to marking yourself . Then some subscripts can go , Some subscripts don't go . Then walk a certain number of steps apart . Under serious circumstances , The first time is to traverse directly . But obviously . This question . It can't be that simple . Because the data he gave was very large . So we need to use a little greedy algorithm . That is, a little mathematical induction . Because every step of frog jumping is fixed . So if the Frog You can reach a plank . Then the position where he jumps out of the board is fixed . Because he started from scratch . And we have the position of the final point of the board . That is, the edge of the board . In this case . Through a formula . Count the steps before the frog jumps out of the plank . That is to say, the coordinates of this board Divide by the number of steps and get the remainder . In this case . This remainder . Subtract steps , Then add the edge of the board . That's where he jumped out of the plank , If this position is smaller than the front of the second board You can end the loop . This is the idea of this topic . It's easy to say , But we still need to think about . Generally speaking , It is a test of your inductive thinking about mathematics .
The code is as follows ,
#include<stdio.h>
int main()
{
long long n,d,m,l,ans,i;
scanf("%lld%lld%lld%lld",&n,&d,&m,&l);
ans=0;
for(i=1;i<=n;i++) {
if(ans<((i-1)*m)) break;
while(ans<=(i-1)*m+l){
ans=d-(((i-1)*m+l)%d)+(i-1)*m+l;
}
}
printf("%lld",ans);
return 0;
}The other topic I want to talk about is the topic of union search . The interesting thing about this topic . It is a knapsack and a parallel search set . Combined topics .
The title is as follows .

On this one . The title says that to buy one, you should match it up and buy it all And there is continuity . It's easy to think of and search sets . Then he asked us what money we had . Get the most value . This is very much like a backpack template . So our idea is to solve the problem by combining search sets and knapsack . The first is the union search . We have their connections . So we can merge it into one . Think of price as space . It's like packing . So I set up a one-dimensional array to record the ancestral relationship . Then we set up a structure . To record prices and values separately . One dimensional array . It is still a normal merge and set lookup operation . The only thing to add is Every merger . Before merging ancestors . The value of the items to be merged . Add the subscript represented by the ancestor In the structure array of , In other words, it is equivalent to packaging the price and value with the ancestors .
And then there's a for Recycle 01 The idea of backpacking . The specific code is as follows ,
#include<stdio.h>
#include<iostream>
using namespace std;
int n,m,w,tot;
int father[1000000],f[10000000];
struct node {
int p, v;
};
node s[1000000],s1[1000000];
int find(int x) {
if(father[x]==x) return x;
father[x]=find(father[x]);
return father[x];
}
void hb(int x,int y) {
int fx=find(x);
int fy=find(y);
if(fx!=fy) {
s1[fx].v+=s1[fy].v;
s1[fx].p+=s1[fy].p;
father[fy]=fx;
}
}
int main() {
cin>>n>>m>>w;
int c,d;
for(int i=1; i<=n; i++) {
cin>>c>>d;
s1[i].p=c;
s1[i].v=d;
father[i]=i;
}
for(int i=1; i<=m; i++) {
int x,y;
cin>>x>>y;
hb(x,y);
}
for(int i=1; i<=n; i++) if(father[i]==i)
{
s[tot]=s1[i];
tot++;
}
for(int i=1; i<=tot; i++){
for(int j=w; j>=s[i].p; j--){
f[j]=max(f[j],f[j-s[i].p]+s[i].v);}}
cout<<f[w];
return 0;
}The last one for A loop is a 01 Backpack template . Here is a little introduction ,01 knapsack . It is a relatively simple idea of dynamic programming . Through a double loop . Put all the items Take them out one by one and go through them . Put it where all this item can be put . Then compare this position , The value of this item . Using a max function . In order to get . The maximum value of items placed in each position . So that's one 01 knapsack . Of course, backpacks have some more complicated situations . This is a relatively simple one .
边栏推荐
- PHP installation SSH2 extension
- numpy 通用函数
- Physical design of database design (2)
- Review of number theory
- "Eight hundred"
- redis集群的方式
- There is no response to redirection and jump in the laravel constructor [original]
- Install SVN in Pagoda and build SVN version Library
- Using jsup to extract images from interfaces
- [learn FPGA programming from scratch -45]: vision chapter - integrated circuits help high-quality development in the digital era -2- market forecast
猜你喜欢
![[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions](/img/39/64df931d5ec54d7d19ae444fa372ba.jpg)
[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions

MySQL index details

2022 talent strategic transformation under the development trend of digital economy

Resolve PHP is not an internal or external command

35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
![[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface](/img/37/84b7d59818e854dac71d6f06700cde.jpg)
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface

Performance test comparison between PHP framework jsnpp and thinkphp6

Construction of art NFT trading platform | NFT mall

Physical design of database design (2)

2021-02-07
随机推荐
PHP small factory moves bricks for three years - interview series - my programming life
Add, delete, modify and query curd in PHP native SQL
redis集群的方式
Clickhouse stand alone installation
Development prospect and investment strategic planning report of global and Chinese PVC hose industry from 2022 to 2028
PHP inherited in class return does not work
BSC 及HT 等链的NFT 创造及绑定图片教程
numpy 数据输入输出
Analysis report on the development trend and operation status of China's environmental monitoring instrument industry from 2022 to 2028
[learn FPGA programming from scratch -45]: vision chapter - integrated circuits help high-quality development in the digital era -2- market forecast
Thinkphp6 implements a simple lottery system
CTF crypto (I) some simple encoding and encryption
SixTool-多功能多合一代挂助手源码
mysql高级学习(跟着尚硅谷老师周阳学习)
Zhubo Huangyu: all the precious metals you want to know are here
Database related knowledge
Thinkphp6 using kindeditor
PHP design function getmaxstr to find the longest symmetric string in a string - [original]
[Qunhui] this suite requires you to start PgSQL adapter service
Navicat connects the pit of shardingsphere sub table and sub library plug-ins