2017 百度之星B轮初赛(Chess, 度度熊的交易计划, 小小粉丝度度熊)

Chess

 
 Accepts: 1805
 
 Submissions: 5738
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 32768/32768 K (Java/Others)
Problem Description

車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子。一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使其互不攻击的方案数。他经过思考,得出了答案。但他仍不满足,想增加一个条件:对于任何一个車A,如果有其他一个車B在它的上方(車B行号小于車A),那么車A必须在車B的右边(車A列号大于車B)。

现在要问问你,满足要求的方案数是多少。

Input

第一行一个正整数T,表示数据组数。

对于每组数据:一行,两个正整数N和M(N<=1000,M<=1000)。

Output

对于每组数据输出一行,代表方案数模1000000007(1e9+7)。

Sample Input
1
1 1
Sample Output
1

思路:

一看到题目就是类似八皇后的水题...敲完直接提交TLE。顿时冷静一看被骗了,其实就是从max(n, m)中选min(n, m)的组合数,即C(min(n, m), max(n, m));

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL mod = 1e9+7;
LL C[1005][1005];
void init()
{
    for(int i = 0; i <= 1000; ++i)
    C[i][0] = 1;
    for(int i = 1; i <= 1000; ++i)
    for(int j = 1; j <= i; ++j)
    C[i][j] = (C[i-1][j]+C[i-1][j-1])%mod;
}
int n, m;
int main()
{
    int t; init();
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &m);
        int minn = min(n, m);
        int maxx = max(n, m);
        printf("%I64d\n", C[maxx][minn]);
    }
    return 0;
}


度度熊的交易计划

 
 Accepts: 460
 
 Submissions: 2329
 Time Limit: 12000/6000 MS (Java/Others)
 
 Memory Limit: 32768/32768 K (Java/Others)
Problem Description

度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题:

喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区。

由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但是最多生产b[i]个。

同样的,由于每个片区的购买能力的区别,第i个片区也能够以c[i]的价格出售最多d[i]个物品。

由于这些因素,度度熊觉得只有合理的调动物品,才能获得最大的利益。

据测算,每一个商品运输1公里,将会花费1元。

那么喵哈哈村最多能够实现多少盈利呢?

Input

本题包含若干组测试数据。 每组测试数据包含: 第一行两个整数n,m表示喵哈哈村由n个片区、m条街道。 接下来n行,每行四个整数a[i],b[i],c[i],d[i]表示的第i个地区,能够以a[i]的价格生产,最多生产b[i]个,以c[i]的价格出售,最多出售d[i]个。 接下来m行,每行三个整数,u[i],v[i],k[i],表示该条公路连接u[i],v[i]两个片区,距离为k[i]

可能存在重边,也可能存在自环。

满足: 1<=n<=500, 1<=m<=1000, 1<=a[i],b[i],c[i],d[i],k[i]<=1000, 1<=u[i],v[i]<=n

Output

输出最多能赚多少钱。

Sample Input
2 1
5 5 6 1
3 5 7 7
1 2 1
Sample Output
23

思路:

很明显的最小费用可行流,建图也挺好建,即新建立一个源点s和汇点t,源点s与每个片区建边,容量为该片区可生产个数,费用为单个成本,然后对所有片区之间建双向边容量为inf,费用为距离,之后再将每个片区与汇点t建边,容量为可卖出个数,费用为负的单价,最后跑一边最小费用流,并将最小费用取负当作答案。

不过还差最关键一步(比赛时没时间改了...),因为我们SPFA寻找的是当前残留网络的一条可改进路,我们是为了最小费用可行流,而非最大流,所以当我们寻找到从s到t的可改进路dis[t] > 0时,此可改进路不会再减少费用,所以需要把最小费用最大流寻找增广路时return的条件改为判断dis[t]是否大于0,ok...

代码:

#include <bits/stdc++.h>  
#define LL long long
using namespace std;  
const LL inf = 0x3f3f3f3f3f3f3f3f;  
const int maxn = 505;
const int maxm = 1e6+maxn*10;  
struct node  
{  
    int v, to, next; 
	LL w, cost; 
}edge[maxm];  
int no, n, m, s, t;  
int head[maxn], vis[maxn], pre[maxn], rec[maxn];
LL dis[maxn];  
queue<int> q;  
inline void init()  
{  
    no = 0;  
    memset(head, -1, sizeof head);  
}  
inline void add(int u, int v, LL w, LL f)  
{  
    edge[no].v = v; edge[no].w = w; edge[no].cost = f;  
    edge[no].next = head[u]; head[u] = no++;  
    edge[no].v = u; edge[no].w = 0; edge[no].cost = -f;  
    edge[no].next = head[v]; head[v] = no++;  
}  
int SPFA(int s, int t)  
{  
    memset(dis, 0x3f3f, sizeof dis);  
    memset(vis, 0, sizeof vis);  
    while(!q.empty()) q.pop();  
    q.push(s); dis[s] = 0; vis[s] = 1;  
    while(!q.empty())  
    {  
        int tp = q.front(); q.pop();  
        vis[tp] = 0;  
        int k = head[tp];  
        while(k != -1)  
        {  
            if(dis[edge[k].v] > dis[tp] + edge[k].cost && edge[k].w)  
            {  
                dis[edge[k].v] = dis[tp] + edge[k].cost;  
                pre[edge[k].v] = tp; rec[edge[k].v] = k;  
                if(vis[edge[k].v] == 0)  
                {  
                    vis[edge[k].v] = 1;  
                    q.push(edge[k].v);  
                }  
            }  
            k = edge[k].next;  
        }  
    }  
    if(dis[t] > 0) return 0;  
    return 1;  
}  
LL Mcmf(int s, int t)  
{  
    LL minflow, mincost = 0, maxflow = 0;  
    int k;
    while(SPFA(s, t))  
    {  
        k = t; minflow = inf;  
        while(k != s)  
        {  
            minflow = min(minflow, edge[rec[k]].w);  
            k = pre[k];  
        }  
        k = t; maxflow += minflow;  
        while(k != s)  
        {  
            mincost += minflow*edge[rec[k]].cost;  
            edge[rec[k]].w -= minflow;  
            edge[rec[k]^1].w += minflow;  
            k = pre[k];  
        }
    }
    return -mincost;  
}  
int main()  
{  
    int a, b, c, d, s, t;
    while(~scanf("%d %d", &n, &m))
	{
		init();
		int s = 501, t = 502;
		for(int i = 1; i <= n; ++i)
		{
			scanf("%d %d %d %d", &a, &b, &c, &d);
			add(s, i, b, a);
			add(i, t, d, -c);
		}
	    for(int i = 1; i <= m; ++i)  
	    {
	        scanf("%d %d %d", &a, &b, &c);  
	        if(a == b) continue;
	        add(a, b, inf, c);
			add(b, a, inf, c);  
	    }
	    printf("%I64d\n", Mcmf(s, t));
	}
    return 0;  
}


小小粉丝度度熊

 
 Accepts: 1075
 
 Submissions: 5191
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 32768/32768 K (Java/Others)
Problem Description

度度熊喜欢着喵哈哈村的大明星——星星小姐。

为什么度度熊会喜欢星星小姐呢?

首先星星小姐笑起来非常动人,其次星星小姐唱歌也非常好听。

但这都不是最重要的,最重要的是,星星小姐拍的一手好代码!

于是度度熊关注了星星小姐的贴吧。

一开始度度熊决定每天都在星星小姐的贴吧里面签到。

但是度度熊是一个非常健忘的孩子,总有那么几天,度度熊忘记签到,于是就断掉了他的连续签到。

不过度度熊并不是非常悲伤,因为他有m张补签卡,每一张补签卡可以使得某一忘签到的天,变成签到的状态。

那么问题来了,在使用最多m张补签卡的情况下,度度熊最多连续签到多少天呢?

Input

本题包含若干组测试数据。

第一行两个整数n,m,表示有n个区间,这n个区间内的天数,度度熊都签到了;m表示m张补签卡。

接下来n行,每行两个整数(l[i],r[i]),表示度度熊从第l[i]天到第r[i]天,都进行了签到操作。

数据范围:

1<=n<=100000

0<=m<=1000000000 0<=l[i]<=r[i]<=1000000000

注意,区间可能存在交叉的情况。

Output

输出度度熊最多连续签到多少天。

Sample Input
2 1
1 1
3 3
1 2
1 1
Sample Output
3
3


    
    
Hint
样例一:度度熊补签第2天,然后第1天、第二天和第三天都进行了签到操作。 样例二:度度熊补签第2天和第3天。

思路:

先将所有能合成的区间合成一个,然后再从新的区间数组里进行尺取即可,注意细节(少写一个q++连TLE几发...)

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e5+5;
struct node
{
	LL l, r;
	bool operator<(const node k)const
	{
		return l < k.l;
	}
} old[maxn], now[maxn];
int n, m, cnt;
LL ans, sum, at;
int main()
{
	while(~scanf("%d %d", &n, &m))
	{
		for(int i = 1; i <= n; ++i)
		scanf("%lld %lld", &old[i].l, &old[i].r);
		sort(old+1, old+n+1);
		cnt = 1;
		now[cnt].l = old[1].l, now[cnt].r = old[1].r;
		for(int i = 2; i <= n; ++i)
		{
			if(old[i].l <= now[cnt].r)
			now[cnt].r = max(now[cnt].r, old[i].r);
			else
			{
				++cnt;
				now[cnt].l = old[i].l, now[cnt].r = old[i].r;
			}
		}
		int q = 1, h = 2;
		sum = now[1].r-now[1].l+1;
		ans = sum+m, at = m;
		while(h <= cnt)
		{
			LL jg = now[h].l-now[h-1].r-1;
			if(at < jg)
			{
				++q;
				while(q < h && at < jg)
				{
					LL jg1 = now[q].l-now[q-1].r-1;
					sum -= now[q-1].r-now[q-1].l+1;
					sum -= jg1, at += jg1;
					++q;
				}
				if(at < jg)
				{
					ans = max(ans, now[h].r-now[h].l+1+at);
					sum = now[h].r-now[h].l+1;
				}
				else
				{
					--q;
					at -= jg, sum += jg;
					sum += now[h].r-now[h].l+1;
					ans = max(ans, sum+at);
				}
			}
			else
			{
				at -= jg, sum += jg;
				sum += now[h].r-now[h].l+1;
				ans = max(ans, sum+at);
			}
			++h;
		}
		printf("%lld\n", ans);
	}
	return 0;
}


继续加油~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值