每日算法 - day 13

每日算法

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


记录下来自己做题时得思路,并不一定是最优解

分糖果

暴力。。。应该会有更好得方法

#include <iostream>
#include <algorithm>

using namespace  std;
const int N = 110;
int n, a[N];
int ans = 0;

void tranmitleft()
{
	int t = a[0] / 2;
	a[0] = a[0] - t;
	for(int i = 0;i < n - 1;i ++)
	{
		a[i] += a[i + 1] / 2;
		a[i+1] /= 2;
	}
	a[n - 1] += t;
	return; 
}

void work()
{
	for(int i = 0;i < n ;i ++)
	{
		if(a[i] % 2 != 0)
		{
			a[i]++;
			ans++;
		}
	}
}

bool check()
{
	int x = a[0];
	for(int i = 0;i < n ;i ++)
	{
		if(x != a[i])return false;
	}
	return true;
}
int main()
{
	cin >> n;
	for(int i = 0;i < n ;i ++)cin >> a[i];
	while(1)
	{
		if(check())break;
		tranmitleft();
		work();
	}
	cout << ans << endl;
	return 0;
}

数字游戏

这道题坑点很多

  1. 精度问题,要考虑数据范围是否溢出。
  2. 观察时间复杂度,应该是一个O(n) 或者 O(longn)得做法,所以一定存在某种技巧
  3. 把数据写下来,推出一般规律
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
const int N = 1000010;
long long n , k , t ;
long long sum = 1;
int main()
{
	cin >> n >> k >> t;
	long long l = 1, r = n, pre = 1;
	for(int i = 1;i < t;i ++)
	{
		pre = (pre + (n*(l+r)/2)) % k;
		sum += pre;
		l += n;
		r += n;			
	}
	cout << sum << endl;
	return 0;
}

luoguP2036 Perket

状态压缩 + dfs

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n ;
struct node
{
	int s , b;
};
vector<node> v;
int ans = 0x3f3f3f;
void dfs(int u, int state) {
    if (u == n) 
	{
		int s = 1,b = 0,flag = 0;
        for (int i = 0; i < n; i ++)
        {
            if (state >> i & 1)
            {
            	flag = 1;
                s *= v[i+1].s;
                b += v[i+1].b;
        	}
    	}
    	if(flag)
		{ 
			ans = min(abs(b-s),ans);
		}
        return ;
    }

    dfs (u + 1, state); 
    dfs (u + 1, state | (1 << u)); 
}
int main()
{
	cin >> n;
	int s, b ;
	v.push_back({0,0});
	for(int i = 1 ;i <= n ;i ++)
	{
		cin >> s >> b;
		v.push_back({s,b});
	} 
	dfs(0,0);
	cout << ans << endl;
	return 0;
}

luogu P1011 车站

| | 1| 2 | 3| 4| ... |
|:-😐:-😐:-😐:-😐:-😐:-😐:-😐
| 上车 | a | b| a+b|a + 2b | | | |
| 下车 | 0 | b| b | a + b| | | |
| 总人数| a|b |2a | 2a + b| | | |

可得:
第i站上车人数 = f[i-2] * a + f[i-1] * b (i >= 2);
第i站下车人数 = f[i-3] * a + f[i-2] * b (i >= 3);
第i站得总人数 = f[i-1]a + a + f[i-1]b - b (i >= 2);

最后一战下车得人数 = 倒数第二站得总人数

我也推出了公式 ,但是却没想到初始化得方法 好菜啊啊啊
使用了0 和 1 来充当系数

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a , n , m , x , b;

int f[25] = {0 , 1};
int main()
{
	cin >> a >> n >> m >> x;
	for(int i = 2;i < n ;i ++)f[i] = f[i-1] + f[i-2];
	
	b = (m - f[n-3]*a - a) / (f[n-2] - 1);
	if(x == 1)cout << a << endl;
	else printf("%d",(f[x-2] + 1) * a + (f[x-1] - 1)*b);
	return 0;
}

posted @ 2020-02-25 17:51  _starsky  阅读(156)  评论(0编辑  收藏  举报