Noip模拟题解题报告

Pro

题目链接

Sol

同花顺

其实就是求一个最长长度,什么的最长长度呢?

如果能组成同花顺,那么一定是同花且连续~~(废话)~~

那么一定满足f[i].b-f[j].b+1<=n

b为上面的数值,只有这样才可能构成同花顺,枚举更新答案就行了。

最后答案是n-maxn

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

struct Node {
	int a , b;
};
Node e[100005] , f[100005];
int n , top , ans , cnt;
inline int mymax(int a , int b) { return a>b?a:b; }

bool cmp(Node a , Node b) {
	if(a.a==b.a)
		return a.b<b.b;
	return a.a<b.a;
}

int main() {
	freopen("card.in","r",stdin);
	freopen("card.out","w",stdout);
	scanf("%d",&n);
	for(int i=1; i<=n; i++)
		scanf("%d%d",&e[i].a,&e[i].b);
	sort(e+1 , e+n+1 , cmp);
	for(int i=1; i<=n; i++) {
		if(e[i].a==e[i-1].a&&e[i].b==e[i-1].b)
			continue;
		f[++top] = e[i];
	}
	for(int i=1; i<=top; i++) {
		cnt = 0;
		for(int j=i; j>=1; j--) {
			if(f[j].a==f[i].a&&f[i].b-f[j].b+1<=n)
				cnt++;
			else
				break;
		}
		ans = mymax(ans , cnt);
	}
	printf("%d",n-ans);
	return 0;
}

做实验

不知道暴力位运算可不可以,反正我没敲出来……

正解:直接枚举子集,然后更新答案。

f[i]=j表示以i表示的2进制的子集也是j的子集,那么就可以根据f[i]来求答案了。

#include<iostream>
#include<cstdio>
using namespace std;

int n , f[100005];

int main() {
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
	scanf("%d",&n);
	for(int i=1; i<=n; i++) {
		int x , y , ans = 0;
		scanf("%d%d",&x,&y);
		for(int j=x; j; j=(j-1)&x) {
			if(f[j]<i-y)
				ans++;
			f[j] = i;
		}
		printf("%d\n",ans);
	}
	return 0;
}

拯救世界

听说递归的tarjan会递归层数过深爆栈……

很裸的一个tarjan+spfa的模板题。

(当然,拓扑也是可做的。)

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;

const int L = 500005;
struct Node {
	int to , next;
};
Node e[L];
int n , m , deep , s , p , from[L] , ans , total[L] , to[L] , val[L] , head[L] , tot , stack[L] , top , low[L] , dfn[L] , cnt , color[L] , vis[L] , dis[L];
inline int mymin(int a , int b) { return a<b?a:b; }
inline int mymax(int a , int b) { return a>b?a:b; }

void add(int x , int y) {
	tot++;
	e[tot].next = head[x];
	e[tot].to = y;
	head[x] = tot;
}

void tarjan(int u) {
	deep++;
	dfn[u] = deep;
	low[u] = deep;
	vis[u] = 1;
	stack[++top] = u;
	for(int i=head[u]; i; i=e[i].next) {
		int v = e[i].to;
		if(!dfn[v]) {
			tarjan(v);
			low[u] = mymin(low[u] , low[v]);
		} else {
			if(vis[v])
				low[u] = mymin(low[u] , low[v]);
		}
	}
	if(low[u]==dfn[u]) {
		color[u] = ++cnt;
		vis[u] = 0;
		while(stack[top+1]!=u) {
			total[cnt] += val[stack[top]];
			vis[stack[top]] = 0;
			color[stack[top]] = cnt;
			top--;
		}
	}
}

void spfa(int s) {
	memset(dis , -0x3f , sizeof(dis));
	memset(vis , 0 , sizeof(vis));
	queue<int>q;
	dis[s] = total[s];
	vis[s] = 1;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = 0;
		for(int i=head[u]; i; i=e[i].next) {
			int v = e[i].to;
			if(dis[v]<dis[u]+total[v]) {
				dis[v] = dis[u] + total[v];
				if(!vis[v]) {
					vis[v] = 1;
					q.push(v);
				}
			}
		}
	}
}

int main() {
	freopen("save.in","r",stdin);
	freopen("save.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1; i<=m; i++) {
		scanf("%d%d",&from[i],&to[i]);
		add(from[i] , to[i]);
	}
	for(int i=1; i<=n; i++)
		scanf("%d",&val[i]);
	for(int i=1; i<=n; i++)
		if(!dfn[i])
			tarjan(i);
	memset(head , 0 , sizeof(head));
	tot = 0;
	for(int i=1; i<=m; i++)
		if(color[from[i]]!=color[to[i]])
			add(color[from[i]] , color[to[i]]);
	scanf("%d%d",&s,&p);
	spfa(color[s]);
	for(int i=1; i<=p; i++) {
		int x;
		scanf("%d",&x);
		ans = mymax(ans , dis[color[x]]);
	}
	printf("%d",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值