每日算法

每日算法

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


P1996 约瑟夫问题

奇葩需求 非要使用单向不循环链表解决该问题 那就试试吧 AC

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

using namespace std;
int n , k;
struct node{
	int id;
	struct node *next = NULL;
};
void work(int n, int k)
{
	node *head = new node;
	head->id = 1;
	node *pre = head, *now = head;
	// 构建单链表
	for(int i = 2;i <= n ;i ++)
	{
		node *tmp = new node;
		tmp->id = i;
		pre->next = tmp;
		pre = tmp;
	}
	node *tail = pre;
	int sum = n, cur = 1;
	while(sum > 1)
	{
		cur++;bool flag = 0;
		if(cur == k)
		{
			flag = 1;
			if(now->next == tail){  //删除最尾巴 
			printf("%d ",now->next->id);  
				tail = now;
				tail->next = NULL;
				cur = 1;sum--; 
				now = head;
			} else if(now->next == NULL){ //删除头节点 
			printf("%d ",head->id);
				head = head->next;
				now = head;
				cur = 1;sum--;
			}else {
			printf("%d ",now->next->id);
				now->next = now->next->next;
				now = now->next;
				cur = 1;sum--;	
			} 
		}
		if(!flag && now != tail)now = now->next;
		else if(!flag && now == tail) now = head;
	}
	printf("%d",head->id);
}
int main()
{
	cin >> n >> k;
	work(n , k);
	return 0;
}

最小生成树 kuskal

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

using namespace std;
const int N = 50005;
const int M = 2000005;
struct node{
	int a , b , v;
};
node a[M];
int n , m , f[N];

int find(int k)
{
	if(f[k] == k)return k;
	else return f[k] = find(f[k]);
}
bool cmp(node a,node b){return a.v < b.v;}
void kuskal()
{
	sort(a + 1,a + 1 + m, cmp);
	int sum = 0,cnt = 1;
	for(int i = 1;i <= n ;i ++)f[i] = i;
	for(int i = 1;i <= m ;i ++)
	{
		int x = find(a[i].a);
		int y = find(a[i].b);
		if(x == y)continue;
		f[x] = y;
		cnt++;sum += a[i].v;
		if(cnt == n)break;
	}
	cout << sum << endl;
}
int main()
{
	cin >> n >> m;
	for(int i = 1;i <= m ;i ++)
	{
		scanf("%d %d %d", &a[i].a, & a[i].b, &a[i].v);
	}
	kuskal();
	return 0;
}

P3908 数列之异或

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <cmath>
using namespace std;
typedef long long ll;
ll  n;
int main()
{
	cin >> n;
	if(n % 4 == 0) cout << n;
	if(n % 4 == 1) cout << 1;
	if(n % 4 == 2) cout << ll( ll (n / 4) * 4) + 3;
	if(n % 4 == 3) cout << 0;
	return 0;
}

P2820 局域网

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

using namespace std;
const int N = 205;
struct node
{
	int a , b;
	int val;
}arr[20005];
int n , m ,f[N], ans , sum , t;


int find(int k)
{
	if(f[k] == k)return k;
	else return f[k] = find(f[k]);
}
bool cmp(node a,node b)
{
	return a.val < b.val;
}
void kuskal()
{
	sort(arr+ 1, arr + 1 + m, cmp);
	// 取边
	for(int i = 1;i <= m ;i ++)
	{
		int x = find(arr[i].a);
		int y = find(arr[i].b);
		if(x == y)continue; //判断这两个边是否在一个集合内
		sum += arr[i].val;  
		f[x] = y;t++;       //如果合法就加入这条边
		if(t == n)return;   //顶点收集完成
	}
}
int main()
{
	cin >> n >> m;
	for(int i = 1;i <= m;i ++)
	{
		scanf("%d %d %d",&arr[i].a,&arr[i].b,&arr[i].val);
		ans += arr[i].val;
	}
	for(int i = 1;i <= m ;i ++)f[i] = i;
	kuskal();
	cout << ans - sum << endl;
	return 0;
}

P1656 炸铁路

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

using namespace std;
const int N = 5005;
int f[N] , n , m;

struct node{
	int a, b ;
	node(int a,int b):a(a), b(b){}
	node(){}
};
node arr[N];
vector<node> ans;
bool cmp(node a,node b)
{
	if(a.a == b.a)return a.b < b.b;
	else return a.a < b. a; 
}
int find(int k)
{
	if(f[k] == k)return k;
	else return f[k] = find(f[k]);
}
int main()
{
	cin >> n >> m;
	for(int i = 0;i < m ;i ++)
	{
		scanf("%d %d",&arr[i].a, &arr[i].b);
	}
	for(int i = 0;i < m ;i ++)
	{
		for(int j = 1;j <= n ;j ++)f[j] = j;
		for(int j = 0;j < m;j ++)
		{
			if(i != j)
			{
				int x = find(arr[j].a);
				int y = find(arr[j].b);
				if(x != y)
				{
					f[x] = y;
				}
			}
		}
		int x = find(arr[i].a) , y = find(arr[i].b);
		if(x != y)
		{
			int a , b;
			if(arr[i].a > arr[i].b)a = arr[i].b, b = arr[i].a;
			else a = arr[i].a,b = arr[i].b;
			ans.push_back(node(a, b));
		}
	}
	sort(ans.begin(),ans.end(),cmp);
	for(int i = 0;i < (int)ans.size();i ++)
	{
		printf("%d %d
",ans[i].a,ans[i].b);
	}
	return 0;
}

P1051 谁拿了最多奖学金

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

using namespace std;
int n ;
struct node{
	string name;
	int sum = 0; //获得的总的奖金数
	int avg , cla;
	bool gan, w;
	int paper;
	int id;
	node(string s,int a,int c,bool g,bool w,int p,int id):
	name(s) , avg(a),cla(c),gan(g),w(w),paper(p),id(id){}
	node(){}
};
vector<node> ans;
bool cmp(node a,node b)
{
	if(a.sum == b.sum)return a.id < b.id;
	else return a.sum > b.sum;
}
int main()
{
	cin >> n;
	string s;
	int a, b ,paper;
	char c , d;
	for(int i = 0;i < n ;i ++)
	{
		cin >> s;
		scanf("%d %d %c %c %d",&a , &b, &c, &d, &paper);
		ans.push_back(node(s,a , b, ((c == 'Y')?1:0),((d == 'Y')?1:0),paper,i));
		if(ans[i].cla > 80 && ans[i].gan){
			ans[i].sum += 850;
		}
		if(ans[i].w && ans[i].avg > 85){
			ans[i].sum += 1000;
		}
		if(ans[i].avg > 90){
			ans[i].sum += 2000;
		}
		if(ans[i].avg > 85 && ans[i].cla > 80){
			ans[i].sum += 4000;
		}
		if(ans[i].avg > 80 && ans[i].paper > 0){
			ans[i].sum += 8000;
		}
	}
	sort(ans.begin(), ans.end(),cmp);
	long long res = 0;
	for(int i = 0;i < (int)ans.size() ;i ++)
	{
		res += ans[i].sum;
	}
	cout << ans[0].name << endl;
	cout << ans[0].sum << endl;
	cout << res << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/wlw-x/p/12623977.html