Daliy Algorithm(二分) — day 55

Nothing to fear

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


打怪

题目本身比较简单,但是所涉及到的小细节稍微多一些,但本意时模拟,看到好多人这道题都挂了 嗯 他们一定是在演我

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

using namespace std;
typedef long long ll;
int h , a , H , A;
int n;
void work()
{
    ll ans = 0;
    // 当毛球的攻击力 <= 0 或者一刀一个毛球
    if(A <= 0 || a >= H){
        cout << -1 << endl;
        return;
    }
    else if(a == 0){ // 当自己攻击力为零时一个都杀不死
        cout << 0 << endl;
        return;
    }else{
        while(h > 0)
        {
            int t = H;
            while(t > 0)
            {
                if(h > 0)t = t - a;  // 毛球被刀
                else break;
                if(t > 0)h = h - A; // 如果毛球还活着你被刀
                else break;
            }
            if(t <= 0 && h > 0)ans++;
        }
        cout << ans << endl;
    }
}
int main()
{
    cin >> n;
    while(n --)
    {
        cin >> h >> a >> H >> A;
        work();
    }
    return 0;
}

四个选项

搜索题 ,主要自己一开始陷入了误区就是利用一张图来存储x 和 y 的限定条件 具体原因还不知道,但是以后最好还是按照规范来操作 还有就是尽量在最后面判断一个答案是否合法,在中间剪枝的话容易混乱导致自己也搞不清楚错在哪里

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

using namespace std;
typedef long long ll;
const int N = 13;
const int M = 1005;
int a[N], m;
ll  ans = 0;
int f[N] , x [M] , y[M];
// 当前第几题 : k 
bool check()
{
    for(int i = 1;i <= m ;i ++)
    {
        if(f[x[i]] != f[y[i]])return false;
    }
    return true;
}

void dfs(int k)
{
    if(k > N)return;
    if(k == N && check()){
        ans++;
        return;
    }
    // 如果存在选项可以选择
    if(a[1] > 0){
        f[k] = 1;a[1]--;dfs(k + 1);a[1]++;f[k] = 0;
    }
    if(a[2] > 0){
        f[k] = 2;a[2]--;dfs(k + 1);a[2]++;f[k] = 0;
    }
    if(a[3] > 0){
        f[k] = 3;a[3]--;dfs(k + 1);a[3]++;f[k] = 0;
    }
    if(a[4] > 0){
        f[k] = 4;a[4]--;dfs(k + 1);a[4]++;f[k] = 0;
    }
}
int main()
{
    cin >> a[1] >> a[2] >> a[3] >> a[4] >> m;
    for(int i = 1;i <= m ;i ++){ 
        scanf("%d %d",&x[i],&y[i]);
    }
    dfs(1);
    cout << ans << endl;
    return 0;
}

水果

贪心 剩下的图论和另外一道题明天再看吧 太菜了

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

using namespace std;
typedef long long ll;
int t;
int n , m;
void work()
{
    ll ans = 0;
    cin >> n >> m;
    if(n > m)
    {
        swap(n , m);    
    }
    while(n != 0)
    {
        if(n * 2 <= m)
        {
            ans++;
            n *= 2;
            continue;
        }
        n -- ; m-- ; ans++;
    }
    cout << ans << endl;
}   
int main()
{
    cin >> t;
    while(t--)
    {
        work();
    }
    return 0;
}

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

欢迎关注

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

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

    Daliy Algorithm(二分) -- day 55

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

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

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

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

(0)
上一篇 2023年3月2日 上午12:58
下一篇 2023年3月2日 上午12:59

相关推荐