每日算法 - 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;
}
posted @ 2020-03-13 21:40  _starsky  阅读(132)  评论(0编辑  收藏  举报