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


    P5198 [USACO19JAN]Icy Perimeter S

    蓝题就这么被AC了 这题确实有点水只需要看出来周长就好了

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    
    using namespace std;
    const int N = 1010;
    
    char a[N][N];
    bool vis[N][N];
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int A = 0,S = 0 , n , ansa = 0, anss = 0;
    
    struct node{
    	int x, y;
    	node(int x,int y):x(x),y(y){}
    };
    void bfs(int sx,int sy)
    {
    	queue<node> q;
    	q.push(node(sx,sy));
    	vis[sx][sy] = 1;
    	
    	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(a[nx][ny] == '.')S++;
    			if(a[nx][ny] == '#' && !vis[nx][ny])
    			{
    				A++;
    				vis[nx][ny] = 1;
    				q.push(node(nx,ny));
    			}
    		}
    		q.pop();
    	}
    }
    
    void input()
    {
    	fill(a[0],a[0] + N * N, '.');
    	cin >> n;
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= n ;j ++)
    		{
    			cin >> a[i][j];
    		}
    	}
    }
    
    int main()
    {
    	input();
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= n ;j ++)
    		{
    			if(!vis[i][j] && a[i][j] == '#')
    			{
    				A = 1;S = 0;
    				bfs(i , j);
    				if(A > ansa)
    				{
    					ansa = A;
    					anss = S;
    				}else if(A == ansa)
    				{
    					if(anss > S)anss = S;
    				}
    			}
    		}
    	}
    	cout << ansa << " " << anss;
    	return 0;
    }
    

    P2068 统计和

    单点修改,区间查询

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #define lowbit(x) (x & -x)
    using namespace std;
    const int N = 100005;
    int tr[N] , a[N] , n , m;
    
    void add(int x,int v)
    {
    	for(int i = x;i <= n ;i += lowbit(i))tr[i] += v;
    }
    
    long long query(int x)
    {
    	long long ans = 0;
    	for(int i = x;i;i-=lowbit(i))
    		ans += tr[i];
    	return ans;
    }
    
    int main()
    {
    	cin >> n >> m;
    	for(int i = 1;i <= n ;i ++)
    	{
    		scanf("%d",&a[i]);
    		add(i,a[i]);	
    	}
    	for(int i = 0;i < m ;i ++)
    	{
    		char ch;int a, b ;
    		cin >> ch;
    		scanf("%d %d",&a,&b);
    		if(ch == 'x'){
    			add(a,b);
    		}else{
    			printf("%lld
    ",query(b)-query(a-1));
    		}
    	}
    	return 0;
    } 
    

    P2360 地下城主

    原题是POJ的题目 被搬过来 做法差不多就是变成一次性的数据了

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <queue>
    
    using namespace std;
    const int N = 105;
    
    
    char a[N][N][N];
    bool vis[N][N][N];
    int l , r, c;
    int sl, sx ,sy;
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int updown[2] = {1,-1};
    struct node{
    	int l , x, y , time;
    };
    
    bool inmap(int ll ,int x,int y)
    {
    	return ll >= 1 && ll <= l && x >= 1 && x <= r && y >= 1 && y <= c;	
    }
    
    bool check(int l , int x , int y)
    {
    	if(inmap(l,x,y) && !vis[l][x][y] && a[l][x][y] != '#')
    	{
    		return true;
    	}else return false;
    }
    
    int bfs()
    {
    	vis[sl][sx][sy] = 1;
    	queue<node> q;
    	q.push({sl, sx ,sy , 0});
    	bool flag = 0;
    	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];
    			int nl = now.l;
    			if(check(nl,nx,ny))
    			{
    				if(a[nl][nx][ny] == 'E')
    				{
    					flag = 1;
    					return now.time + 1;
    				}
    				vis[nl][nx][ny] = 1;
    				q.push({nl,nx,ny,now.time + 1});	
    			}	
    		}
    		// 值改变当前层数
    		for(int i = 0;i < 2;i ++)
    		{
    			int nx = now.x,ny = now.y;
    			int nl = now.l + updown[i];
    			if(check(nl,nx,ny))
    			{
    				if(a[nl][nx][ny] == 'E')
    				{
    					flag = 1;
    					return now.time + 1;
    				}
    				vis[nl][nx][ny] = 1;
    				q.push({nl,nx,ny,now.time + 1});	
    			}	
    		} 
    		q.pop();
    	}
    	if(!flag) return -1;
    }
    
    void output(int ans)
    {
    	if(ans == -1)
    	{
    		cout << "Trapped!" << endl;
    		return;	
    	}else{
    		printf("Escaped in %d minute(s).
    ",ans);
    	}
    }
    
    void input()
    {
    	string s;
    	for(int k = 1;k <= l;k ++)
    	{
    		for(int i = 1;i <= r ;i ++)
    		{
    			cin >> s;
    			for(int j = 0;j < s.size() ;j ++)
    			{
    				a[k][i][j+1] = s[j];
    				if(a[k][i][j+1] == 'S')
    				{
    					sl = k;sx = i;sy = j + 1;		
    				}
    			}
    		}	
    	}	
    }
    void clear()
    {
    	for(int k = 1;k <= l;k ++)
    	{
    		for(int i = 1;i <= r ;i ++)
    		{
    			for(int j = 1;j <= c ;j ++)
    			{
    				vis[k][i][j] = 0;
    			}
    		}	
    	}
    }
    int main()
    {
    	cin >> l >> r >> c;
    	input();
    	int dis = bfs();
    	output(dis);
    	clear();
    	return 0;
    }
    

    CF580C Kefa and Park

    嗯 这最后搜的时候其实是判断是否到达最后一个还满足条件

    #include<bits/stdc++.h>
    
    using namespace std;
    const int N = 1000005;
    
    int arr[N] , n , m ;
    long long ans = 0;
    vector<int> a[N];
    bool vis[N];
    
    void dfs(int now,int val)
    {
    	if(val > m)return;
    	int t = 1;
    	vis[now] = 1;
    	for(int i = 0;i < a[now].size();i++)
    	{
    		if(!vis[a[now][i]])
    		{
    			t = 0;	
    			if(!arr[a[now][i]])
    			{
    				dfs(a[now][i],0);
    			}else dfs(a[now][i],val + 1);
    		}
    	}
    	ans += t;
    	return;
    }
    
    void input()
    {
    	cin >> n >> m;
    	for(int i = 1;i <= n;i ++)
    		scanf("%d",&arr[i]);
    	for(int i = 0;i < n - 1;i ++)
    	{
    		int x , y;
    		scanf("%d %d",&x , &y);
    		a[x].push_back(y);
    		a[y].push_back(x); 
    	}
    }
    int main()
    {
    	input();
    	dfs(1,arr[1]);
    	cout << ans << endl;
    	return 0;
    }
    
  • 相关阅读:
    2016年11-29 mysql数据库
    2016年11月25日网页项目知识
    11月22日 网页项目遇到知识
    2016年11月15
    document操作
    2016年11月8日 函数
    2016年11月7日 数组练习
    2016年11月6日数组
    2016年11月4日运算符与语句
    2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12566974.html
Copyright © 2011-2022 走看看