codeforces1369E DeadLee

本文深入解析了一种基于食物分配的算法实现,通过ned数组记录每种食物的需求人数,w数组记录食物供应上限,利用队列维护已满足条件的食物,确保在资源有限的情况下,尽可能多地满足需求,避免资源浪费。

https://codeforces.com/problemset/problem/1369/E

ned[x]为当前这个食物要被多少人吃

如果所有ned[x]>w[x],那么无论怎么安排,最后还是不够

如果某个ned[x]<=w[x],说明我们可以把吃x的人放到最后,那么这样他们最终至少会有一个人吃,在放人的同时,我们ned进行减操作,如果某个时刻ned[i]<=w[i],说吃第i种食物的人也可以放在当前已经放了的人除外的最后了,那么加入队列,用个队列维护吃哪些食物的人已经可以放最后了,然后维护ned[i]的大小,顺便放人。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=3e5+10;

int n,m,cas,k,cnt,tot,ans;
int a[maxl][2],w[maxl],ned[maxl];
vector<int> b[maxl],c;
char s[maxl];
bool in[maxl],vis[maxl]; 
queue<int> q;

inline void prework()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",&w[i]),vis[i]=false;
	int x,y;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		a[i][0]=x;a[i][1]=y;
		ned[x]++;ned[y]++;
		b[x].push_back(i);
		b[y].push_back(i);
		in[i]=false;
	}
} 

inline void mainwork()
{
	ans=0;
	for(int i=1;i<=n;i++)
	if(ned[i]<=w[i])
		ans=1,q.push(i),vis[i]=true;
	if(!ans) 
		return;
	int x,y,tot=0,u;
	while(!q.empty())
	{
		u=q.front();q.pop();tot++;
		for(int v:b[u])
		if(!in[v])
		{
			c.push_back(v);in[v]=true;
			x=a[v][0];y=a[v][1];
			--ned[x];--ned[y];
			if(ned[x]<=w[x] && !vis[x])
			{
				q.push(x);
				vis[x]=true;
			}
			if(ned[y]<=w[y] && !vis[y])
			{
				q.push(y);
				vis[y]=true;
			}
		}
	}
	int len=c.size();
	if(tot!=n || len!=m)
		ans=0;
}

inline void print()
{
	if(!ans)
		puts("DEAD");
	else
	{ 
		puts("ALIVE");
		int len=c.size();
		for(int i=len-1;i>=0;i--)
			printf("%d%c",c[i]," \n"[i==0]);
	}
}

int main()
{
	int t=1;
	//scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值