Daliy Algorithm (graph,思维)-- day 59

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


A

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

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
typedef long long ll;
int t;

void work()
{
	ll n;
	cin >> n;
	if(n & 1)cout << n / 2 << endl;
	else cout << n / 2 - 1 << endl;
}
int main()
{
	cin >> t;
	while(t--)
	{
		work();
	}
	return 0;
}

B

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

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
const int N = 2005;
typedef long long ll;
int t , n , a , b;
int vis[30], ans[2000];
void work()
{
	cin >> n >> a >> b;
	memset(vis, 0 , sizeof vis);
	memset(ans, 0 , sizeof ans);
	for(int i = 1;i <= b;i ++)ans[i] = i;
	for(int i = 1;i <= b;i ++)vis[ans[i]]++;

	for(int i = b + 1;i <= n ;i ++)
	{
		int pos = 0;
		vis[ans[max(1,i - a)]]--;
		for(int j = 1;j <= b;j ++)
		{
			if(vis[j] <= 0)
			{
				pos = j;
				break;
			}
		}
		if(pos != 0)ans[i] = pos;
		else ans[i] = 1;

		vis[ans[i]]++;
	}
	for(int i = 1;i <= n ;i ++)cout << (char)(ans[i] + 'a' -1);
	cout << endl;
}
int main()
{
	SIS;
	cin >> t;
	while(t--)
	{
		work();
	}
	return 0;
}

C

统计每一个数字出现的次数
记录出现次数最多的数字的个数

  1. map 同时记录存在多少个数字
  2. 如果最大连续的个数 == 一共存在多少种数字 那么ans = 连续 - 1
  3. 否则ans = min(最大连续的个数,一共存在多少种)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <map>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
const int N = 100005;
typedef long long ll;
int t , n ;
map<int,int> a;
void work()
{
	cin >> n;
	int x = 0 , maxc = 0;
	for(int i = 1;i <= n ;i ++)
	{
		cin >> x;
		a[x]++;
		if(a[x] > maxc)
		{
			maxc = a[x];
		}
	}
	int y = a.size();
	if(maxc == y)maxc--;
	cout << min(maxc , y) << endl;
	a.clear();
	return;
} 
int main()
{
	SIS;
	cin >> t;
	while(t--)
	{
		work();
	}
	return 0;
}

无向图的最小环问题

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

using namespace std;
const int N = 105;
const int M = 5005;
const int MAX = 0x3f3f;
int f[M][M];
int g[M][M];
int n , m;
void floyd()
{
	int ans = MAX;
	for(int k = 1;k <= n ;k ++)
	{
		for(int i = 1;i < k ;i ++)
		{
			for(int j = i + 1;j < k ;j ++)
			{
				ans = min(ans , g[i][j] + f[i][k] + f[k][j]);
			}
		}
		for(int i = 1;i <= n ;i ++)
		{
			for(int j = 1;j <= n ;j ++)
			{
				g[i][j] = min(g[i][j],g[i][k] + g[k][j]),g[j][i] = g[i][j];
			}
		}
	}
	if(ans == MAX)
	{
		cout << "No solution." << endl;
	}else cout << ans << endl;
}

void init()
{
	for(int i = 1;i <= n ;i ++)
	{
		for(int j = 1;j <= n ;j ++)
		{
			if(i == j)f[i][j] = g[i][j] = 0;
			else f[i][j] = g[i][j] = MAX;
		}
	}
}
int main()
{
	cin >> n >> m;
	init();
	int a, b ,c;
	for(int i = 1;i <= m;i ++)
	{
		scanf("%d %d %d",&a ,&b,&c);
		g[a][b]=g[b][a]=min(g[a][b],c);
        f[a][b]=f[b][a]=min(f[a][b],c);
	}
	floyd();
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/12723358.html