每日算法 - day 34
每日算法
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.20
luogu- P1478 陶陶摘苹果(升级版)
思路: 贪心 + 模拟即可对于每一次摘苹果的力气消耗进行排序,消耗小的排在前面。。。最后再从头开始能拿就拿
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
int n , s , a , b;
struct node{
int x , y ;
};
vector<node> v;
bool cmp(node a,node b)
{
if(a.y == b.y)
{
return a.x < b.x;
}else return a.y < b.y;
}
int main()
{
cin >> n >> s >> a >> b;
for(int i = 0;i < n ;i ++)
{
int x , y;
cin >> x >> y;
v.push_back({x , y});
}
sort(v.begin(),v.end(),cmp);
int ans = 0;
for(int i = 0;i < v.size() ;i ++)
{
if(v[i].x <= b && s - v[i].y >= 0)
{
ans++;
s -= v[i].y;
continue;
}
if(v[i].x <= a + b && s - v[i].y >= 0)
{
ans ++;
s -= v[i].y;
}
}
cout << ans << endl;
return 0;
}
luogu- P1515 旅行
感觉算法的边界越来越模糊,可能时我太菜了只会写暴力 + 贪心 hh,听说这道题也可以用dp
写搜索之前一定要把分支的条件卡好,不然还要浪费时间去调试就会很麻烦
回头记得再去看一下vector容器的初始化,真的是很久没用STL了好多都忘了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
int n , a , b;
int arr[] = {0,990,1010,1970,2030,2940, 3060
,3930,4060,4970, 5030, 5990, 6010, 7000};
vector<int> v;
int ans = 0;
void dfs(int dis,int day,int id)
{
if(dis == 7000)
{ //到达终点 表示此方案成立
ans++;
return;
}
for(int i = id;i < v.size() ;i ++)
{
if( (v[i] - dis )>= a && (v[i] - dis)<= b)
{
dfs(v[i],day + 1,i + 1); //day + 1 表示住下了
}
}
}
int main()
{
cin >> a >> b;
cin >> n;
for(int i = 0;i < 14 ;i ++ )v.push_back(arr[i]);
for(int i = 0;i < n ;i ++)
{
int x;cin >> x;
v.push_back(x);
}
sort(v.begin(),v.end());
dfs(0 , 0 , 0);
cout << ans << endl;
return 0;
}
luogu -P1332 血色先锋队
怀疑时评测机器抽风下载出来的数据格式都不对,根本没办法跑数据,也不知道时卡哪里了,反正思路就是这么个思路也不知道他卡哪里
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
using namespace std;
const int N = 1000;
int n , m , a , b;
int map[N][N] , dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int vis[N][N];
struct node{
int x, y,t;
};
vector<node> leader;
vector<node> inflect;
bool inmap(int x, int y)
{
return x >= 1 && x <= n && y >= 1 && y <= m;
}
// 如何判断这个点是不是
bool check(int x,int y)
{
if(inmap(x , y))
{
if(vis[x][y] != -1)return 1;
else return 0;
}else return 0;
}
// 感染传递的过程
void bfs(node t)
{
queue<node> q;
q.push(t);
while(!q.empty())
{
node now = q.front();
for(int i = 0;i < 4;i ++)
{
int nx = now.x + dir[i][0];
int ny = now.y + dir[i][1];
if(check(nx, ny))
{
if(map[nx][ny] == 0) //表示这个点还没来过
{
map[nx][ny] = now.t + 1;
q.push({nx, ny,map[nx][ny]});
}else{
if(map[nx][ny] > now.t + 1)
{
map[nx][ny] = now.t + 1;
q.push({nx, ny,map[nx][ny]});
}
}
}
}
q.pop();
}
}
int main()
{
scanf("%d%d%d%d\n",&n ,&m,&a,&b);
int x = 0, y = 0;
for(int i = 0; i < a ;i ++)
{
scanf("%d %d",&x , &y);
vis[x][y] = -1;
inflect.push_back({x , y , 0});
}
for(int i = 0;i < inflect.size() ;i ++)
{
bfs(inflect[i]);
}
for(int i = 0;i < b ;i ++)
{
scanf("%d %d",&x ,&y);
printf("%d\n",map[x][y]);
}
return 0;
}