2016长乐夏令营 Day4

T1:

模拟。。开一排const char

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

const char n0[5][3] = {' ','-',' ','|',' ','|',' ',' ',' ','|',' ','|',' ','-',' '};
const char n1[5][3] = {' ',' ',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' '};
const char n2[5][3] = {' ','-',' ',' ',' ','|',' ','-',' ','|',' ',' ',' ','-',' '};
const char n3[5][3] = {' ','-',' ',' ',' ','|',' ','-',' ',' ',' ','|',' ','-',' '};
const char n4[5][3] = {' ',' ',' ','|',' ','|',' ','-',' ',' ',' ','|',' ',' ',' '};
const char n5[5][3] = {' ','-',' ','|',' ',' ',' ','-',' ',' ',' ','|',' ','-',' '};
const char n6[5][3] = {' ','-',' ','|',' ',' ',' ','-',' ','|',' ','|',' ','-',' '};
const char n7[5][3] = {' ','-',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' '};
const char n8[5][3] = {' ','-',' ','|',' ','|',' ','-',' ','|',' ','|',' ','-',' '};
const char n9[5][3] = {' ','-',' ','|',' ','|',' ','-',' ',' ',' ','|',' ','-',' '};

char ch[200];
int k,s;

void Work(int i,int t)
{
				if (t == 0) {
					printf("%c",n0[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n0[i][1]);
					printf("%c",n0[i][2]);
				}
				else if (t == 1) {
					printf("%c",n1[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n1[i][1]);
					printf("%c",n1[i][2]);
				}
				else if (t == 2) {
					printf("%c",n2[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n2[i][1]);
					printf("%c",n2[i][2]);
				}
				else if (t == 3) {
					printf("%c",n3[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n3[i][1]);
					printf("%c",n3[i][2]);
				}
				else if (t == 4) {
					printf("%c",n4[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n4[i][1]);
					printf("%c",n4[i][2]);
				}
				else if (t == 5) {
					printf("%c",n5[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n5[i][1]);
					printf("%c",n5[i][2]);
				}
				else if (t == 6) {
					printf("%c",n6[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n6[i][1]);
					printf("%c",n6[i][2]);
				}
				else if (t == 7) {
					printf("%c",n7[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n7[i][1]);
					printf("%c",n7[i][2]);
				}
				else if (t == 8) {
					printf("%c",n8[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n8[i][1]);
					printf("%c",n8[i][2]);
				}
				else if (t == 9) {
					printf("%c",n9[i][0]);
					for (int l = 0; l < k; l++) printf("%c",n9[i][1]);
					printf("%c",n9[i][2]);
				}
				for (int l = 0; l < s; l++) printf(" ");
	
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
		freopen("test.txt","w",stdout); 
    #else
		freopen("adraw.in","r",stdin);
		freopen("adraw.out","w",stdout);
	#endif
	
	cin >> k >> s;
	scanf("%s",ch);
	int len = strlen(ch);
	for (int i = 0; i < 5; i++) {
		if (i % 2 == 0) {
			for (int j = 0; j < len; j++) {
				int t = ch[j] - '0';
				Work(i,t);
			}	
			printf("\n");
		}
		else 
			for (int l = 0; l < k; l++) {
				for (int j = 0; j < len; j++) {
					int t = ch[j] - '0';
					Work(i,t);
				}	
				printf("\n");
			}
	}
	return 0;
}


T2:

可以发现,无论这两个函数按什么顺序复合,始终是2^k*x + 2^k-1的形式,而k <= 30W,枚举一遍取最优即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

typedef long long LL;
const LL mo = 1000000007LL;
const int maxn = 1E5 + 10;

LL n,k1[maxn],b1[maxn],k2[maxn],b2[maxn];
int f[maxn*3],bo[maxn*3]; 

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
    #else
		freopen("number.in","r",stdin);
		freopen("number.out","w",stdout);
	#endif
	
	cin >> n; bo[0] = bo[2] = 1; f[2] = 1;
	for (int i = 3; i <= 300000; i++) {
		f[i] = ~0U>>1;
		if (bo[i-2]) bo[i] = 1,f[i] = min(f[i],f[i-2]+1);
		if (bo[i-3]) bo[i] = 1,f[i] = min(f[i],f[i-3]+1);
	}
	int ans = ~0U>>1;
	LL mi = 2;
	for (int i = 2; i <= 300000; i++) {
		mi = mi*2LL%mo;
		LL now = (n*mi%mo + mi-1LL+mo)%mo;
		if (now % mo == 0) ans = min(ans,f[i]);
	} 
	cout << ans;
	return 0;
}

T3:

先BFS算出每个人跑向各个洞口所需的时间设为t,把每个洞口拆成T个点,每个人向对应洞口的第t到T个点连边,最后一遍最大流即可

现场没写出来,写的时候每个人没有连t+1到T的点

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 200;
const int maxm = 3E6 + 30; 
const int cx[4] = {0,1,0,-1};
const int cy[4] = {1,0,-1,0};

struct P{
	int x,y;
	P(int _x = 0,int _y = 0){x = _x; y = _y;}
};

struct E{
	int to,cap,flow,from;
	E(int _to = 0,int _cap = 0,int _flow = 0,int _from = 0){to = _to; cap = _cap; flow = _flow; from = _from;}
}edgs[maxm*2];

char p[20][20];
int ans,n,m,t,h,d,dis[20][20],ti[maxn][maxn],hx[maxn],hy[maxn],dx[maxn],dy[maxn]
	,cur,tot,tail[maxm],dn[maxn][maxn],L[maxm],last[maxm];
bool vis[maxm];

queue <int> q;

void Add(int x,int y,int w)
{
	edgs[cur] = E(y,w,0,tail[x]); tail[x] = cur++;
	edgs[cur] = E(x,0,0,tail[y]); tail[y] = cur++;
}

bool BFS()
{
	for (int i = 0; i <= tot; i++) L[i] = 0;
	q.push(0); L[0] = 1; 
	while (!q.empty()) {
		int k = q.front(); q.pop();
		for (int i = tail[k]; i != -1; i = edgs[i].from) {
			if (edgs[i].flow == edgs[i].cap || L[edgs[i].to]) continue;
			L[edgs[i].to] = L[k] + 1;
			q.push(edgs[i].to);
		}
	}
	return L[tot];
}

int Dicnic(int x,int a)
{
	if (!a || x == tot) return a;
	int flow = 0;
	for (int &i = last[x]; i != -1; i = edgs[i].from) {
		if (L[edgs[i].to] - L[x] != 1) continue;
		if (edgs[i].cap == edgs[i].flow) continue;
		int f = Dicnic(edgs[i].to,min(a,edgs[i].cap - edgs[i].flow));
		if (f) {
			a -= f;
			flow += f;
			edgs[i].flow += f;
			edgs[i^1].flow -= f;
			if (!a) return flow;
		}
	}
	return flow;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
    #else
		freopen("escape.in","r",stdin);
		freopen("escape.out","w",stdout);
	#endif
	
	for (int i = 0; i <= 300000; i++) tail[i] = -1;
	cin >> n >> m >> t;
	for (int i = 0; i < n; i++) {
		scanf("%s",p[i]);
		for (int j = 0; j< m; j++) {
			if (p[i][j] == 'O') {
				++d; dx[d] = i; dy[d] = j; 
			}
			if (p[i][j] == 'P') {
				++h; hx[h] = i; hy[h] = j; 
				Add(0,h,1);
			}
		}
	}
	tot = h;
	for (int i = 1; i <= d; i++)
		for (int j = 1; j <= t; j++)
			dn[i][j] = ++tot;
	++tot;
	for (int i = 1; i <= d; i++)
		for (int j = 1; j <= t; j++)
			Add(dn[i][j],tot,1);
	for (int i = 1; i <= h; i++) {
		for (int x = 0; x < n; x++)
			for (int y = 0; y < m; y++)
				dis[x][y] = ~0U>>1;
		dis[hx[i]][hy[i]] = 0;
		queue <P> Q;
		Q.push(P(hx[i],hy[i]));
		while (!Q.empty()) {
			P k = Q.front(); Q.pop();
			for (int j = 0; j < 4; j++) {
				int xx = k.x + cx[j];
				int yy = k.y + cy[j];
				if (xx < 0 || xx == n || yy < 0 || yy == m) continue;
				if (dis[xx][yy] != ~0U>>1 || p[xx][yy] == '*') continue;
				dis[xx][yy] = dis[k.x][k.y] + 1;
				Q.push(P(xx,yy));
			}
		}
		for (int j = 1; j <= d; j++) 
			if (dis[dx[j]][dy[j]] < ~0U>>1)
				for (int l = dis[dx[j]][dy[j]]; l <= t; l++)
					Add(i,dn[j][l],1);
	}
	
	while (BFS()) {
		for (int i = 0; i <= tot; i++) last[i] = tail[i];
		ans += Dicnic(0,~0U>>1);
	}
	cout << ans;
	return 0;
}

T4:

最小生成树+LCA,模板题

写的时候存边的数组没乘2。。。为了长乐的老爷机没有用vector的后果

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 3E4 + 10;

struct E{
	int x,y,w;
	bool operator < (const E &b) const {
		return w < b.w;	
	}
	E(int _x = 0,int _y = 0,int _w = 0){x = _x; y = _y; w = _w;}
}edgs[maxn*10];

int cur,ans,n,m,f[maxn],fa[maxn][20],cost[maxn][20],
	tail[maxn],to[maxn],v[maxn],from[maxn],L[maxn];

int fat(int x)
{
	return x == f[x]?x:f[x] = fat(f[x]);
}

void Add(int x,int y,int w)
{
	from[++cur] = tail[x];
	to[cur] = y; v[cur] = w;
	tail[x] = cur;
	from[++cur] = tail[y];
	to[cur] = x; v[cur] = w;
	tail[y] = cur;
}

void dfs(int x)
{
	for (int i = 1; i < 20; i++) {
		fa[x][i] = fa[fa[x][i-1]][i-1];
		cost[x][i] = max(cost[x][i-1],cost[fa[x][i-1]][i-1]);
	}
	for (int i = tail[x]; i; i = from[i]) {
		if (L[to[i]]) continue;
		L[to[i]] = L[x] + 1;
		fa[to[i]][0] = x;
		cost[to[i]][0] = v[i];
		dfs(to[i]);
	}
}

void LCA(int p,int q)
{
	if (L[p] < L[q]) swap(p,q);
	int Log; 
	for (Log = 0; L[p] - (1<<Log) >= 1; Log++); --Log;
	for (int j = Log; j >= 0; j--)
		if (L[p] - (1<<j) >= L[q]) {
			ans = max(ans,cost[p][j]);
			p = fa[p][j];
		}
	if (p == q) return;
	for (int j = Log; j >= 0; j--)
		if (fa[p][j] != fa[q][j]) {
			ans = max(ans,cost[p][j]);
			ans = max(ans,cost[q][j]);
			p = fa[p][j]; q = fa[q][j];
		} 
	ans = max(ans,cost[q][0]);
	ans = max(ans,cost[p][0]);
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
    #else
		freopen("cost.in","r",stdin);
		freopen("cost.out","w",stdout);
	#endif
	
	cin >> n >> m;
	for (int i = 1; i <= n; i++) f[i] = i;
	for (int i = 0; i < m; i++) {
		int x,y,z; scanf("%d%d%d",&x,&y,&z);
		edgs[i] = E(x,y,z);
	}
	sort(edgs,edgs + m);
	for (int i = 0; i < m; i++) {
		int fx = fat(edgs[i].x);
		int fy = fat(edgs[i].y);
		if (fx != fy) {
			f[fx] = fy;
			Add(edgs[i].x,edgs[i].y,edgs[i].w);
		}
	}
	L[1] = 1; dfs(1);
	int t; cin >> t;
	while (t--) {
		int x,y; scanf("%d%d",&x,&y);
		ans = 0; LCA(x,y);
		printf("%d\n",ans);
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值