每日算法 – day 28

每日算法

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


lqb 日志统计

贪心 + 双指针

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <set> 
using namespace std;
const int N = 100005;

int n , d , k;
struct node{
    int ts,id;
};
vector<node> v;
set<int> ans;
bool cmp(node a,node b)
{
    if(a.id == b.id)
    {
        return a.ts < b.ts;
    }else return a.id < b.id;
}
int main()
{

    cin >> n >> d >> k;
    int a , b;
    for(int i = 0;i < n ;i ++)
    {
        scanf("%d %d",&a , &b);
        v.push_back({a , b});
    } 
    sort(v.begin() , v.end(), cmp);

    int j = 1;
    for(int i = 0;i < v.size() ;i ++)
    {
        j = i + 1;
        //printf("%d %d\n",v[i].id , v[i].ts);
        while(v[i].id == v[j].id)
        {
            if(v[j].ts - v[i].ts <= d - 1)
            {
                if(j - i + 1 >= k)
                {
                    ans.insert(v[i].id);
                    break;
                }
            }
            j++;
        }
    }
    for(auto it = ans.begin(); it != ans.end(); it++)
    {
        cout << *it << endl; 
    } 
    return 0;
} 

lqb 交换瓶子

超级大暴力

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

using namespace std;
const int N = 100005;
int f[N]  , n ;
int main()
{
    cin >> n;

    for(int i = 1;i <= n ;i ++)
    {
        scanf("%d",&f[i]);
    }
    int ans = 0;

    for(int i = 1;i <= n ;i ++)
    {
        if(f[i] != i)
        {
            for(int j = i + 1;j <= n ;j ++)
            {
                if(f[j] == i)
                {
                    int t = f[i];
                    f[i] = f[j];
                    f[j] = t;
                    ans++;
                    break;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

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

欢迎关注

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

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

    每日算法 - day 28

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

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

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

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

(0)
上一篇 2023年3月1日 下午10:01
下一篇 2023年3月1日 下午10:02

相关推荐