Daily algorithm - day 33

Daily algorithm

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


Those spare your hard work pay, you do not want to train, when you feel tired but still have to adhere to teeth when that chasing a dream, do not care what the end there, the road to enjoy the process, perhaps you can not dream of success, but there will be more great things will follow. mamba out ~

2020.3.19


luogu- p1095 Rye escape

dp + greedy, of course, the search is not over, and can not prune too. Too much data, the second iteration of weight change, although it will retain the original structure but which one is the best of three sub-structure, it does not affect the nature of optimal substructure of dp. Finally, pay attention to border initialization, greed greed is actually able to flash on the flash

#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\n%d",i);
            return 0;
        }
    }
    printf("No\n%d",f[t]);
    return 0;
} 

luogu -p1123 fetch game

Each number only two states, election or no election, the election terrible last words to restore the state. Since it is an enumeration of all time thanks to a number enumeration, enumeration type can be compared with the index

#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 nuts Bowling

Also get greedy, greedy why I can only sample too QAQ
cry, give me Shetenglebantian was finally complete the task today.

#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;
} 

Guess you like

Origin www.cnblogs.com/wlw-x/p/12526998.html