每日算法 - day 31
每日算法
those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out
那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~
2020.3.17
luogu -P1253 线性存储问题
贪心 贪速度,\(f = v / t\)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 10005;
int n;
struct node{
int f;
int id;
};
vector<node> v;
bool cmp(node a,node b)
{
return a.f > b.f;
}
int main()
{
cin >> n;
int a, b ;
for(int i = 0;i < n ;i ++)
{
scanf("%d %d",&a ,&b);
v.push_back({a * b, i + 1});
}
sort(v.begin(),v.end(),cmp);
for(int i = 0;i < v.size() ;i ++)
{
cout << v[i].id << " ";
}
return 0;
}
luogu -P1324 矩形分割
同样是贪心但是同时贪一次性切分的权重和切分次数乘机最小
因为权值越大的越早切开之后会减少之后权值很大的被一直切分
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;
const int N = 2005;
int n , m;
int col[N],row[N];
bool cmp(int a,int b)
{
return a > b;
}
int main()
{
cin >> n >> m;
for(int i = 1;i < n ;i ++)
{
scanf("%d",&row[i]);
}
for(int i = 1;i < m ;i ++)
{
scanf("%d",&col[i]);
}
sort(row + 1,row + n , cmp);
sort(col + 1,col + m , cmp);
long long ans = 0;
int r = 1, c = 1;
for(int i = 2;i < n + m ;i ++)
{
if(row[r] > col[c])ans += c * row[r++];
else ans += r * col[c++];
}
cout << ans << endl;
return 0;
}
luogu -P1294 高手散步
裸的图的 dfs遍历(邻接矩阵)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 55;
int map[N][N];
int n , m;
bool vis[N];
int ans = -99999;
void dfs(int now,int fav)
{
ans = max(fav,ans);
for(int i = 1;i <= n ;i ++)
{
if(!vis[i] && map[now][i] > 0)
{
vis[i] = 1;
dfs(i,fav + map[now][i]);
vis[i] = 0;
}
}
}
int main()
{
cin >> n >> m;
int a, b , c;
for(int i = 0;i < m ;i ++)
{
cin >> a >> b >> c;
map[a][b] = c;
map[b][a] = c;
}
for(int i = 1;i <= n ;i ++)
{
vis[i] = 1;
dfs(i,0);
vis[i] = 0;
//fill(vis,vis + N, 0);
}
cout << ans << endl;
return 0;
}