Daliy Algorithm (cf , GPLT )-- day 90

Nothing to fear


种一棵树最好的时间是十年前,其次是现在!

那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.6.28


人一我十,人十我百,追逐青春的梦想,怀着自信的心,永不言弃!

GPLT-L2-009 抢红包

  1. 如果出现double类型进行比较大小可能是坑,尽可能想办法根据题意看能否
    转化为int类型进行比较
  2. 使用vecotr容器在pat中更好使
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
const int N = 10005;

int n;
struct node{
	int id;
	int money; //元位单位
	int cnt;
};

bool vis[N];

bool cmp(node a,node b)
{
	if(a.money != b.money)return a.money > b.money;
	else if(a.cnt != b.cnt)return a.cnt > b.cnt;
	else return a.id < b.id;
}
int main()
{
	cin >> n;
	vector<node> ans(n + 1);
	for(int who = 1;who <= n;who ++)
	{
		int k , id ,mount;
		cin >> k;
		ans[who].id = who;
		for(int i = 0;i < k;i ++)
		{
			cin >> id >> mount;
			if(!vis[id])
			{
				ans[id].money += mount ;
				ans[who].money -= mount;
				ans[id].cnt++;
			}
			vis[id] = true; 
		}
		memset(vis , 0 , sizeof vis);
	}
	sort(ans.begin() + 1, ans.end(), cmp);
	for(int i = 1;i <= n;i ++)
	{
		printf("%d %.2f\n",ans[i].id, (double)ans[i].money * 1.0 / 100);
	}
	return 0;
}

GPLT -L2-010 排座位

思路: 根据输入的信息建立图模型
利用一个二维数组直接存两个客人之间的关系。

如果两个人关系为敌人时,需要检测一下两个人是否存在共同的朋友
此时用dfs去检查一下从一个点能够只走关系为1的边走到目标那里即可

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

using namespace std;
const int N = 105;
vector<int> mapp[N];
int g[N][N];
int n , m , k;

bool vis[N];
void check(int s, int t, bool &flag)
{
	if(s == t){
		flag = true;
		return;
	}
	for(int i = 0;i < mapp[s].size();i ++)
	{
		int next = mapp[s][i];
		if(!vis[next] && g[s][next] == 1 && !flag)
		{
			vis[next] = 1;
			check(next , t , flag);
			vis[next] = 0;
		}
	}
}

int main()
{	
	cin >> n >> m >> k;
	for(int i = 0;i < m;i ++)
	{
		int a , b , c;
		scanf("%d %d %d",&a , & b, &c);
		g[a][b] = c;g[b][a] = c;
		mapp[a].push_back(b);
		mapp[b].push_back(a);
	}
	for(int i = 0;i < k;i ++)
	{
		int a, b;
		scanf("%d %d",&a , &b);
		if(g[a][b] == 1)
			printf("No problem\n");
		if(g[a][b] == 0)
			printf("OK\n");
		if(g[a][b] == -1)
		{
			bool flag = false;
			check(a , b , flag);
			if(flag)
			{
				printf("OK but...\n");
			}else printf("No way\n");
		}
	}
	return 0;
}

GPLT-L2-011 玩转二叉树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

struct node{
	int val;
	struct node *left = NULL;
	struct node *right = NULL;
};
int n;
vector<int> in , pre, level;
int find(int x)
{
	for(int i = 0;i < in.size();i++)
	{
		if(x == in[i])return i;
	}
}
node* build(int x,int y,int p,int q)
{
	if(x > y || p > q)return NULL;
	int i = find(pre[x]); // 在中序遍历中找到当前先序区间中的第一个
	int k = i - p;
	node *now = new node;
	now->val = pre[x];
	now->right = build(x + 1, x + k, p , i - 1);
	now->left = build(x + k + 1, y, i + 1,  q);
	return now;
}


void bfs(node *root)
{
	queue<node*> q;
	q.push(root);
	while(!q.empty())
	{
		node *now = q.front();q.pop();
		level.push_back(now->val);
		if(now->left != NULL)q.push(now->left);
		if(now->right != NULL)q.push(now->right);
	}
	return;
}

int main()
{
	cin >> n;
	in.resize(n),pre.resize(n);
	for(int i = 0;i < n;i ++)
		cin >> in[i];
	for(int i = 0;i < n;i ++)
		cin >> pre[i];
	node *tree = build(0, n-1,0,n-1);
	bfs(tree);
	for(int i = 0;i < n;i ++)
	{
		if(i == 0)printf("%d",level[i]);
		else printf(" %d",level[i]);
	}
	return 0;
}

650Div3 A

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cassert>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
using namespace std;
typedef long long ll;
const int MAX = 1e+9;
int t;

void slove()
{
	ll x , y , n;
	cin >> x >> y >> n;
	ll l = 0, r = MAX;
	while(l < r)
	{
		ll mid = l + r + 1>> 1;
		if(mid * x + y <= n)l = mid;
		else r = mid - 1;
	}	
	cout << l * x + y << endl;	
}
int main()
{
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed; // 在iomanip中
#endif
	SIS;
	cin >> t;
	while(t--)
	{
		slove();
	}
#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}

650Div3 B

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cassert>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
using namespace std;
typedef long long ll;
const int MAX = 0x7ffffff;
int t;

void slove()
{
	int n;
	cin >> n;
	int ans = 0;
	while(n != 1)
	{
		while(n % 6 == 0)
		{
			n /= 6;
			ans++;
		}
		int cnt = 0;
		while(n % 6 != 0 && n != 1)
		{
			cnt++;n *= 2;
			if(cnt > 1){
				cout << -1 << endl;
				return;
			}
			ans++;
		}
	}
	cout << ans << endl;
}
int main()
{
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed; // 在iomanip中
#endif
	SIS;
	cin >> t;
	while(t--)
	{
		slove();
	}
#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}

650Div3 C

手生的一批

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cassert>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
using namespace std;
typedef long long ll;
const int MAX = 0x7ffffff;
int t , n;
void slove()
{ 
	int n;
	string s;
	cin >> n >> s;
	int ans = 0 , cnt = 0;
	for(char c : s)
	{
		cnt += (c == ')' ? 1 : -1);
		ans = max(ans , cnt);
	}
	cout << ans << endl;
}
int main()
{
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed; // 在iomanip中
#endif
	SIS;
	cin >> t;
	while(t--)
	{
		slove();
	}
#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
posted @ 2020-06-29 10:06  _starsky  阅读(83)  评论(0编辑  收藏  举报