zoukankan      html  css  js  c++  java
  • 每日算法

    每日算法

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


    luogu-P2669 金币

    简单模拟

    #include <iostream> 
    #include <algorithm>
    #include <string>
    #include <cstdio>
    typedef long long ll;
    using namespace std;
    const int N = 10002;
    int k;
    ll  ans;
    int main()
    {
    	cin >> k;
    	int i = 1,j = 1;
    	while(i <= k)
    	{
    		int t = j;
    		while(t-- && i <= k){
    			ans += j;i++;
    		}
    		j++; 
    	}
    	cout << ans << endl;
    	return 0;
    }
    

    luogu-P1451 求细胞数量

    dfs 求连通图

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cstdio>
    
    using namespace std;
    const int N = 110;
    int a[N][N] , n , m;
    bool vis[N][N];
    int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
    long long ans , cnt;
    
    bool inmap(int x,int y)
    {
    	return (x >= 1 && y >= 1 && x <= n && y <= m);
    }
    
    void dfs(int x,int y)
    {
    	for(int i = 0;i < 4;i ++)
    	{
    		int nx = x + dir[i][0];
    		int ny = y + dir[i][1];
    		if(inmap(nx,ny) && vis[nx][ny] == 0 && a[nx][ny] > 0)
    		{
    			vis[nx][ny] = 1;cnt++;
    			dfs(nx,ny);
    		}
    	}
    }
    int main()
    {
    	cin >> n >> m;
    	string line;
    	for(int i = 1;i <= n ;i ++)
    	{
    		cin >> line;
    		for(int j = 0;j < line.size();j++)
    		{
    			a[i][j+1] = line[j] - '0'; 
    		}
    	}
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= m ;j ++)
    		{
    			if(!vis[i][j] && a[i][j] != 0)
    			{
    				cnt = 1;dfs(i,j);ans++;
    			}
    		}
    	}
    	cout << ans << endl;
    	return 0;
    }
    

    luogu- P1101 单词方阵

    dfs + 剪枝

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cstdio>
    #include <vector>
    
    using namespace std;
    const int N = 110;
    int n ;
    int dir[9][2] = {{0,0},{1,0},{-1,0},{0,1},{0,-1},
    {1,1},{-1,-1},{1,-1},{-1,1}};
    
    char map[N][N];
    bool vis[N][N];
    string format = "yizhong";
    
    struct node{
    	int x , y;
    };
    
    bool inmap(int x,int y)
    {
    	return (x >= 1 && x <= n && y >= 1 && y <= n);
    }
    
    void dfs(int x,int y,int now,string s,vector<node> path,int direct)
    {
    	if(s == format)
    	{
    		for(int i = 0;i < path.size(); i++)
    		{
    			vis[path[i].x][path[i].y] = 1;
    		}
    		return;
    	}else{
    		for(int i = 1;i <= 8; i++)
    		{
    			int nx = x + dir[i][0];
    			int ny = y + dir[i][1];
    			if(inmap(nx,ny))
    			{
    				if(format[now] == map[nx][ny])     //剪枝 
    				{                                              
    					if(direct == 0){                    //剪枝 
    						path.push_back({nx,ny});
    						dfs(nx,ny,now+1,s+format[now],path,i);
    						path.pop_back();
    					}else if(direct == i)
    					{
    						path.push_back({nx,ny});
    						dfs(nx,ny,now+1,s+format[now],path,i);
    						path.pop_back();
    					}
    				}
    			}
    		}
    	}
    }
    
    void outans()
    {
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= n ;j ++)
    		{
    			if(vis[i][j])printf("%c",map[i][j]);
    			else printf("*");
    		}
    		puts("");
    	}
    }
    int main()
    {
    	cin >> n;string s;
    	for(int i = 1;i <= n ;i ++)
    	{
    		cin >> s;
    		for(int j = 0;j < s.size(); j++)
    			map[i][j+1] = s[j];
    	}
    	
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= n ;j ++)
    		{
    			if(map[i][j] == 'y')
    			{
    				vector<node> path;
    				path.push_back({i,j});
    				dfs(i,j,1,"y",path,0);	
    			}	
    		}	
    	}	
    	outans();
    	return 0;
    } 
    

    luogu-P1105 平台

    大佬说把他从高往看成一条线,看落在那种颜色的线上就可以判断最终落在了哪里,而且题目中的数据会出现重叠情况!!思路可以细细品

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cstdio>
    #include <vector>
    using namespace std;
    const int N = 20202;
    
    struct node{
    	int no,h,l,r,ansl,ansr;
    }a[10101];
    int ans[N];
    
    int n ; 
    
    bool cmp1(node a,node b)
    {
    	if(a.h == b.h)return a.no > b.no;
    	return a.h < b.h;
    }
    bool cmp2(node a,node b)
    {
    	return a.no < b.no;
    }
    int main()
    {
    	
    	cin >> n;
    	for(int i = 0;i < n ;i ++)
    	{
    		a[i].no = i + 1;
    		cin >> a[i].h >> a[i].l >> a[i].r;
    	}
    	
    	sort(a,a+n,cmp1);
    	for(int i = 0;i < n ;i ++)
    	{
    		a[i].ansr = ans[a[i].r];
    		a[i].ansl = ans[a[i].l];
    		
    		for(int j = a[i].l + 1;j <= a[i].r - 1;j++)
    		{
    			ans[j] = a[i].no;
    		}
    	}
    	
    	sort(a,a+n,cmp2);
    	for(int i = 0;i < n ;i ++)
    	{
    		printf("%d %d
    ",a[i].ansl,a[i].ansr);
    	}
    	return 0;
    }
    

    luogu -P1706 全排列问题

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <iomanip>
    using namespace std;
    const int N = 10;
    int n,a[N];
    bool vis[N];
    void dfs(int now)
    {
    	if(now == n)
    	{
    		for(int i = 0;i < n; i++)
    		{
    			cout << setw(5) << a[i];
    		}
    		cout << endl;
    	}
    	for(int i = 1;i <= n;i ++)
    	{
    		if(!vis[i])
    		{
    			a[now] = i;vis[i] = 1;
    			dfs(now+1);vis[i] = 0;
    		}
    	}
    }
    int main()
    {
    	cin>> n;
    	dfs(0);
    	return 0;
    } 
    
  • 相关阅读:
    CF1208C
    CF1208B
    CF1208A
    CF1206A
    wqy的C题
    wqy的B题
    [POI2005]SAM-Toy Cars
    Gym
    操作系统学习---进程
    C++多线程(POSIX)
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12385370.html
Copyright © 2011-2022 走看看