每日算法 – day 43

每日算法

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


我太菜了 今天开始补cfdiv3

CF#629Div3 A

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
int n;
int main()
{
    cin >> n;
    int a, b ;
    while(n --) 
    {
        cin >> a >> b;
        if(a % b == 0)printf("0\n");
        else{ 
            printf("%d\n",b - (a % b));
        }
    }
    return 0;
} 

CF#629Div3 B

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    int t, ii, n, k, sum, i, j;
    scanf("%d", &t);
    for (ii = 0; ii<t; ii++) 
    {
        scanf("%d %d", &n, &k);
        sum = 0;
        k--;
        for (i = 0;; i++)
        {
            if (sum + i + 1 > k)
            {
                break;
            }
            sum += i + 1;
        }
        k -= sum;
        i++;
        for (j = 0; j<n; j++) 
        {
            if (j == n - i - 1 || j == n - k - 1)
            {
                printf("b");
            }
            else
            {
                printf("a");
            }
        }
        printf("\n");
    }
    return 0;
}

CF#629Div3 C

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

void func()
{
    int n;
    cin >> n;
    string s , a , b;
    cin >> s;
    int flag = 0;
    for(int i = 0;i < s.size() ;i ++)
    {
        if(s[i] == '2')
        {
            if(flag == 0)
                a+='1',b+='1';
            else 
                a+='0',b+='2';  
        }
        else if(s[i] == '0')a+='0',b+='0';
        else{
            if(flag == 0)
            {
                flag = 1;
                a+='1';b+='0'; 
            }
            else{
                a += '0';b += '1';
            }
        }   
    }
    cout << a << endl;
    cout << b << endl;  
}
int main()
{
    int t;cin >> t;
    while(t--)
    {
        func(); 
    }   
    return 0;
} 

摆动序列

假dp 真搜索

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
int n, ans;
bool vis[25];
void dfs(int a ,int b)
{
    if(b > a){
        for(int i = 1;i < a ;i ++)
        {
            if(!vis[i])
            {
                ans++;
                vis[i] = 1;
                dfs(b , i);
                vis[i] = 0;
            }
        }
    }
    else if(a > b){ 
        for(int i = a + 1;i <= n;i ++)
        {
            if(!vis[i])
            {   
                ans++;
                vis[i] = 1;
                dfs(b , i);
                vis[i] = 0;
            }
        }
    }
}
int main()
{
    cin >> n;
    for(int i = 1;i <= n ;i ++)
    {
        vis[i] = 1;
        for(int j = 1;j <= n ;j ++)
        {
            if(i != j)
            {
                ans++;
                vis[j] = 1;
                dfs(i , j);
                vis[j] = 0;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

拦截导弹

\(o(n^{2})\)

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
const int N = 100005;
int f[N], a[N], n;

int main()
{
    while(~scanf("%d",&a[++n]));n--;
    int ans1 = 0, ans2 = 0;
    for(int i = n;i >= 1 ;i --)
    {
        f[i] = 1;
        for(int j = i + 1;j <= n ;j ++)
        {
            if(a[j] <= a[i])
                f[i] = max(f[i],f[j] + 1);
        }
        ans1 = max(ans1,f[i]);
    }
    for(int i = 1;i <= n ;i ++)
    {
        f[i] = 1;
        for(int j = 1;j < i ;j ++)
        {
            if(a[j] < a[i])
                f[i] = max(f[i],f[j] + 1);
        }
        ans2 = max(ans2,f[i]);
    }
    printf("%d\n%d",ans1,ans2);
    return 0;
}

原文链接: https://www.cnblogs.com/wlw-x/p/12595469.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    每日算法 - day 43

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/338611

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午11:41
下一篇 2023年3月1日 下午11:41

相关推荐