牛客算法周周练8

小A买彩票
链接:https://ac.nowcoder.com/acm/contest/11483/A
来源:牛客网

题目描述
小A最近开始沉迷买彩票,并且希望能够通过买彩票发家致富。已知购买一张彩票需要3元,而彩票中奖的金额分别为1,2,3,4元,并且比较独特的是这个彩票中奖的各种金额都是等可能的。现在小A连续购买了n张彩票,他希望你能够告诉他至少能够不亏本的概率是多少。
输入描述:
一行一个整数N,为小A购买的彩票数量一行一个整数N,为小A购买的彩票数量
输出描述:
输出一个最简分数a/b,表示小A不亏本的概率。
\若概率为1,则输出1/1,概率为0,则输出0/1。输出一个最简分数a/b,表示小A不亏本的概率。
若概率为1,则输出1/1,概率为0,则输出0/1。
示例1
输入
复制
2
输出
复制
3/8
备注:
0 \leq n \leq 300≤n≤30

一开始爆搜了一下,然后发现是dp,以100为不赚不亏,表示出第i个物品当前赚的或者亏的是多少的方案数,最后放到一块即可

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1e5 + 10;
const int M = 35;
const int mod = 1e9 + 7;
const int PP = 131;
const double eps = 1e-10;

int n;
int up, down;
int state[M];
int f[M][210];

int gcd(int a, int b){
	return b ? gcd(b, a % b) : a;
}


void dfs(int u){
	if (u == n){
		int sum = 0;
		for (int i = 0; i < n; i ++){
			if (state[i] == 1)   sum -= 2;
			if (state[i] == 2)   sum -= 1;
			if (state[i] == 4)   sum += 1;
		}
		if (sum >= 0)   up ++;
		else   down ++;
	//	cout << up << " " << down << endl;
		return;
	}
	
	for (int i = 1; i <= 4; i ++){
		state[u] = i;
		dfs(u + 1);
		state[u] = 0;
	}
}

signed main(){
	gt(n);
	
	if (n == 0){
		cout << "1/1" << endl;
		return 0;
	}
	else if (n == 1){
		cout << "1/2" << endl;
		return 0;
	}
	else{
	//	cout << up << " " << down << endl;
		f[1][99] = 1;
		f[1][98] = 1;
		f[1][100] = 1;
		f[1][101] = 1;
		for (int i = 2; i <= n; i ++){
			for (int j = 1; j <= 200; j ++){
				f[i][j] += f[i - 1][j - 1];
				f[i][j] += f[i - 1][j];
				f[i][j] += f[i - 1][j + 1];
				f[i][j] += f[i - 1][j + 2];
			}
		}
		
		for (int i = 1; i <= 200; i ++){
			if (i >= 100){
				up += f[n][i];
			}
			down += f[n][i];
		} 
		
		int GCD = gcd(up, down);
		
		cout << up / GCD << "/" << down / GCD << endl;
	}
	
	return 0;
}

「金」点石成金
链接:https://ac.nowcoder.com/acm/contest/11483/B
来源:牛客网

题目描述
赛时提示:魔法值和财富值初始为0

帕秋莉掌握了一种金属性魔法
她决定去捡一些石头,施展点石成金魔法

帕秋莉将捡到的n块石头排成一排,并决定将一些石头点为黄金

对于第i块石头,如果将其变为黄金,会增加ai的财富,消耗bi的魔法(需要说明的是,就算魔法值不够,也可以操作,操作后魔法值归零)

否则,帕秋莉将会回复ci的魔法,但减少di的财富(财富值同理,可以无限制减少)

帕秋莉想知道,按照1-n的顺序以此操作每块石头,如何决策,可以使自己最后的收益值最大
只需要输出最大收益
收益值=财富值*魔法值

(提示:数值不会变为负数,即任何时候,如果数值小于了0,它会立即变为0)

输入描述:
第一行一个整数n
接下来n行,每行四个数,分别代表对应石头的a,b,c,d值
输出描述:
一个整数表示答案
示例1
输入
复制
2
1926 817 2003 627
1949 1001 1234 4321
输出
复制
1952898
备注:
对于20%的数据,1≤n≤2
对于100%的数据,1≤n≤15,0≤ai,bi,ci,di≤1,000,000

贪心找性质,找方法没有找到发现数据很小直接爆搜

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1e5 + 10;
const int M = 35;
const int mod = 1e9 + 7;
const int PP = 131;
const double eps = 1e-10;

int n;
int ans;
int a[M], b[M], c[M], d[M];

void dfs(int u, int mo, int cai){
	if (u == n + 1){
		int sum = mo * cai;
		ans = max(ans, sum);
		return;
	}
	
	//cout << u << "------" << mo << "---------" << cai << endl;

	dfs(u + 1, max(0ll, mo - b[u]), cai + a[u]);
	dfs(u + 1, mo + c[u], max(0ll, cai - d[u]));
}

signed main(){
	gt(n);
	
	for (int i = 1; i <= n; i ++){
		gt(a[i]);
		gt(b[i]);
		gt(c[i]);
		gt(d[i]);
	}
	
	dfs(1, 0, 0);
	
	cout << ans << endl;
	
	return 0;
}

Board
链接:https://ac.nowcoder.com/acm/contest/11483/E
来源:牛客网

题目描述
恬恬有一个nx n的数组。她在用这个数组玩游戏:
开始时,数组中每一个元素都是0。
恬恬会做某些操作。在一次操作中,她可以将某一行的所有元素同时加上一个值,也可以将某一列的所有元素同时加上一个值。
在几次操作后,一个元素被隐藏了。你能帮助她回忆隐藏的数是几吗?
输入描述:
第一行一个整数n(1≤ n≤ 1000)。
接下来n行每行n个整数表示数组a。
第(i+1)行的第j个元素表示aij(aij=-1或0≤ aij ≤ 10000)。-1表示隐藏的元素。
输出描述:
仅一个整数表示答案。
示例1
输入
复制
3
1 2 1
0 -1 0
0 1 0
输出
复制
1

除了-1在的那一行那一列剩下的都减去当前行列的最小值,最后答案是那一行的最大值和那一列的最大值的和,过程中一直二维差分。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1010;
const int M = 35;
const int mod = 1e9 + 7;
const int PP = 131;
const double eps = 1e-10;

int n;
int ans;
int a[N][N], b[N][N];
int col_min[N], row_min[N];

void insert(int x1, int y1, int x2, int y2, int x){
	b[x1][y1] += x;
	b[x2 + 1][y1] -= x;
	b[x1][y2 + 1] -= x;
	b[x2 + 1][y2 + 1] += x;
}

signed main(){
	gt(n);
	int row, col;
	memset(row_min, 0x3f, sizeof row_min);
	memset(col_min, 0x3f, sizeof col_min);
	
	for (int i = 1; i <= n; i ++){
		for (int j = 1; j <= n; j ++){
			int x;
			scanf("%lld", &x);
			if (x == -1){
				row = i;
				col = j;
				continue;
			}
			row_min[i] = min(row_min[i], x);
		//	col_min[j] = min(col_min[j], x);
			insert(i, j, i, j, x);
		}
	}
	
	
	for (int i = 1; i <= n; i ++){
		if (i == row)   continue;
		insert(i, 1, i, n, -row_min[i]);
	}
	
	/*	for (int i = 1; i <= n; i ++){
		for (int j = 1; j <= n; j ++){
			b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
			cout << b[i][j] << "--";
		}
		cout << endl;
	}*/
	
	for (int i = 1; i <= n; i ++){
		for (int j = 1; j <= n; j ++){
			b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
			col_min[j] = min(col_min[j], b[i][j]);
		}
	}
	
	memcpy(a, b, sizeof b);
	memset(b, 0, sizeof b);
	
	for (int i = 1; i <= n; i ++){
		for (int j = 1; j <= n; j ++){
			insert(i, j, i, j, a[i][j]);
		}
	}
	
	for (int i = 1; i <= n; i ++){
		if (i == col)  continue;
		insert(1, i, n, i, -col_min[i]);
	}
	
	for (int i = 1; i <= n; i ++){
		for (int j = 1; j <= n; j ++){
			b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
		}
	}
	
	int ans1 = 0;
	for (int i = 1; i <= n; i ++)    ans1 = max(ans1, b[row][i]);
	int ans2 = 0;
	for (int i = 1; i <= n; i ++)     ans2 = max(ans2, b[i][col]);
	
	cout << ans1 + ans2 << endl;
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值