算法竞赛进阶指南学习day11

CH2201

思路:深搜所有装猫的情况,更新最小值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
long long x[50];
long long wmax;
long long w[50];//表示每车重量
int n;
int minn=100;
bool cmp(int a,int b) {
	return a>b;
}
void dfs(int num,int now) { //num为当前所装车数,now,为当前装了几只
	if(num>=minn) return ;
	if(now==n+1) {
		minn=min(minn,num);
		//cout<<minn<<endl;
		return ;
	}

	for(int i=1; i<=num; i++) {//每次进来都寻找可以装当前猫的车,若能装,则继续在这个里面装 
		if(w[i]+x[now]<=wmax) { //能装
			w[i]+=x[now];
			dfs(num,now+1);
			w[i]-=x[now];
		}
	}
	w[num+1]=x[now];//发现最后有的猫哪个车都装不了,单开一个车; 
	dfs(num+1,now+1);
	w[num+1]=0;
}
int main() {
	cin>>n>>wmax;
	for(int i=1; i<=n; i++) {
		cin>>x[i];
	}
	sort(x+1,x+n+1,cmp);
	dfs(0,0);
	cout<<minn;
	return 0;
}

/*
5 1996
1
2
1994
12
29



*/

关于bitset(不会手打的福音,有自带库!)

转自https://blog.csdn.net/ywh15387127537/article/details/88707044

定义:bitset<位数> n;

位运算都可以用: 与、或、非、异或,左移,右移
foo&foo2
foo|foo2
~foo
foo^foo2
foo<<=2
foo>>=2
foo.size() 返回大小(位数)
foo.count() 返回1的个数
foo.any() 返回是否有1
foo.none() 返回是否没有1
foo.set() 全都变成1
foo.set(p) 将第p + 1位变成1
foo.set(p, x) 将第p + 1位变成x
foo.reset() 全都变成0
foo.reset(p) 将第p + 1位变成0
foo.flip() 全都取反
foo.flip(p) 将第p + 1位取反
foo.to_ulong() 返回它转换为unsigned long的结果,如果超出范围则报错
foo.to_ullong() 返回它转换为unsigned long long的结果,如果超出范围则报错
foo.to_string() 返回它转换为string的结果

CH2101

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<bitset>
using namespace std;
int n, m;
int hd[30005], nxt[30005], to[30005], tot;
bitset<30005> f[30005];
int x, y;

inline void Add( int x, int y ) {//链式前向星存图 
	nxt[++tot] = hd[x];
	hd[x] = tot;
	to[tot] = y;
}

void dfs( int x ) {
	if ( f[x].any() ) return;
	f[x][x] = 1;
	for ( int i = hd[x]; i; i = nxt[i] ){
		dfs( to[i] );
		f[x] |= f[to[i]];
	}
		
}

int main() {
	cin>>n>>m;
	for ( int i = 1; i <= m; ++i ) {
		cin>>x>>y;
		Add( x, y );
	}
	for ( int i = 1; i <= n; ++i ){
		dfs(i);
		cout<<f[i].count()<<endl;
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值