• 每日算法


    每日算法

    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.19


    luogu- p1095 守望者的逃离

    dp + 贪心,搜索当然是过不了的,剪枝也过不了。数据量太大,第二重迭代虽然会改变原来的结构但是却保留的是三种子结构的最优的哪一个,所以并不影响dp的最优子结构的性质。最后边界初始化要注意,贪心贪的其实就是能闪就闪

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <cstdio>
    
    using namespace std;
    const int N = 300005;
    int m , s , t ;
    int f[N]; 
    /*
        将原问题分解为: 每秒钟能够移动的最大距离 
        1. 魔法瞬移  +60m, 魔法 m - 10
    	2. 按照跑步  17 m/s
    	3. 原地休息  恢复魔法的速率为 4点/s
    */
    int main()
    {
    	cin >> m >> s >> t;
    	for(int i = 1;i <= t; ++i)
    	{
    		if(m >= 10){
    			f[i] = f[i-1] + 60;
    			m-=10;
    		}else{
    			f[i] = f[i - 1];
    			m += 4;
    		}
    	}
    	
    	for(int i = 1;i <= t ; ++i)
    	{
    		if(f[i] < f[i - 1] + 17)f[i] = f[i - 1] + 17;
    		if(f[i] >= s)
    		{
    			printf("Yes
    %d",i);
    			return 0;
    		}
    	}
    	printf("No
    %d",f[t]);
    	return 0;
    } 
    

    luogu -p1123 取数游戏

    每种数只有两种状态,选或者不选,选了得话要最后恢复状态。既然枚举所有得数就一次枚举,可以对比指数型枚举

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <vector>
    
    using namespace std;
    const int N = 10;
    
    int a[N][N] , n , m , t;
    int vis[N][N]; 
    int dir[8][2] = {{1,0},{0,1},{-1,0},{0,-1},
    {1,1},{1,-1},{-1,1},{-1,-1}};
    int ans = 0;
    
    void turn(int x,int y,bool flag) 
    {
    	for(int i = 0;i < 8;i ++)
    	{
    		int nx = x + dir[i][0];
    		int ny = y + dir[i][1];
    		if(flag)vis[nx][ny] ++;
    		else vis[nx][ny]--;
    	}
    }
    
    void dfs(int x,int y,int val)
    {
    	if(y == m + 1)
    	{
    		dfs(x + 1, 1 , val);
    		return;
    	}
    	if(x == n + 1)
    	{
    		ans = max(val , ans);
    		return;
    	}
    	
    	dfs(x , y + 1,val); // 不取这个数
    	
    	if(!vis[x][y])
    	{
    		turn(x , y , 1);
    		dfs(x , y + 1 , val + a[x][y]);
    		turn(x , y , 0);
    	} 
    }
    
    void input()
    {
    	 cin >> n >> m;
    	 for(int i = 1;i <= n ;i ++)
    	 {
    	 	for(int j = 1;j <= m ;j ++)
    	 	{
    	 		cin >> a[i][j];
    		}
    	 }
    }
    
    void clear()
    {
    	fill(a[0],a[0] + N * N,0);
    	fill(vis[0],vis[0] + N * N, 0);
    	ans = 0;
    }
    
    int main()
    {
    	cin >> t;
    	while(t--)
    	{
    		input();
    		dfs(1,1,0);
    		cout << ans << endl;
    		clear();
    	}
    	return 0;
    } 
    

    luogu -P1413 坚果保龄球

    同样得贪心,为什么我的贪心就只能过样例QAQ
    哭了,给我折腾了半天总算是完成今天得任务。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct node{
    	int row,time;
    };
    vector<node> v;
    int n;
    bool vis[7];
    bool cmp(node a,node b)
    {
    	if(a.row == b.row)
    	{
    		return a.time < b.time;
    	}else return a.row < b.row;
    }
    int main()
    {
    	cin >> n;
    	int a, b ;
    	for(int i = 0;i < n ;i ++)
    	{
    		cin >> a >> b;
    		v.push_back({a , b});
    	}	
    	sort(v.begin(),v.end(),cmp);
    	long long ans = 0;
    	int r = v[0].row, t = v[0].time;
    	for(int i = 1;i < v.size() ;i ++)
    	{
    		if(v[i].row != r)
    		{
    			ans++;
    			r = v[i].row;
    			t = v[i].time;
    			continue;	
    		}
    		if(v[i].time - t >= 60)
    		{
    			ans++;
    			t = v[i].time;	
    		}	
    	}
    	cout << ans + 1 << endl;
    	return 0;
    } 
    
  • 相关阅读:
    WPF:ListView 分组合并
    WPF:ListView 分页
    SQL
    多线程27 -- ReentrantLock
    String 对象常用方法及属性 详细介绍
    js中Array 对象方法 详细介绍
    js--运算符与或非 及 if判断条件、隐式转换 介绍
    js-函数表达式和函数声明的区别
    js预编译案例分析
    js--万物皆对象
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12526998.html
Copyright © 2020-2023  润新知