uva11111 Generalized Matrioshkas (模拟 + 栈)

题意:类似于括号匹配, 不过每个数字只能跟自己的相反数匹配。并且, 括号的所有直接子括号的数值之和必须小于它的数值。


思路:用栈模拟, 遇到负数入栈; 正数则与栈顶匹配, 若匹配成功则消去栈顶 , 否则也入栈。栈的元素是结构体, 除了上面的括号自身的数值value外, 还有所有直接子括号的和sum。 在负数入栈前, 要把它的数值加到栈顶(如果empty就不用)的sum, 并判断, 判断完才能入栈。


算法复杂度:n个括号 操作n次。 所以是o(N)。


代码:

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

struct Num
{
	int value;
	int childSum;
};

stack<Num> sta;
bool ok;
int value;
char ch;

void init();
void doOnce();
bool judge();

int main()
{

	init();
	while (scanf("%d%c", &value, &ch) == 2) {
		if (ch == '\n')	 {
			doOnce();
			if (judge()) {
				printf(":-) Matrioshka!\n");
			} else {
				printf(":-( Try again.\n");
			}
			init();
		} else {
			doOnce();
		}
	}

	return 0;
}

void init()
{
	while (!sta.empty()) {
		sta.pop();
	}
	ok = true;
}

void doOnce()
{
	Num tp;
	tp.value = value;
	tp.childSum = 0;

	if (value < 0) {
		if (sta.empty()) {
			sta.push(tp);
		} else {
			sta.top().childSum += tp.value;
			if (sta.top().childSum <= sta.top().value) {
				ok = false;
			}
			sta.push(tp);
		}
	} else {
		if (sta.empty()) {
			ok = false;
		} else if (sta.top().value == -tp.value) {
			sta.pop();
		} else {
			sta.push(tp);
		}
	}
}

bool judge()
{
	if (ok && sta.empty()) {
		return true;
	} else {
		return false;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Generalized Extreme Value(广义极值分布)是一种连续型概率分布,通常用于描述极端值的分布。它的概率密度函数为:$$f(x)=\frac{1}{\sigma}\Bigg[\frac{x-\mu}{\sigma}\Bigg]^{-1-\xi}e^{-\Big[\frac{x-\mu}{\sigma}\Big]^{-\xi}}$$ 其中,$\mu$是分布的位置参数,$\sigma>0$是分布的尺度参数,$\xi$是分布的形状参数。 下面是一些与GEV分布相关的操作: 1. GEV分布的概率密度函数 ```python import numpy as np import matplotlib.pyplot as plt from scipy.stats import genextreme loc, scale, shape = 0.5, 2, -0.1 # 分布的参数 x = np.linspace(-2, 6, 1000) pdf = genextreme.pdf(x, shape, loc, scale) # GEV分布的概率密度函数 plt.plot(x, pdf, label='shape = '+str(shape)) # 绘制概率密度函数曲线 plt.xlabel('x') plt.ylabel('pdf(x)') plt.legend() plt.show() ``` 2. GEV分布的累积分布函数 ```python import numpy as np import matplotlib.pyplot as plt from scipy.stats import genextreme loc, scale, shape = 0.5, 2, -0.1 # 分布的参数 x = np.linspace(-2, 6, 1000) cdf = genextreme.cdf(x, shape, loc, scale) # GEV分布的累积分布函数 plt.plot(x, cdf, label='shape = '+str(shape)) # 绘制累积分布函数曲线 plt.xlabel('x') plt.ylabel('cdf(x)') plt.legend() plt.show() ``` 3. GEV分布的样本随机数生成 ```python import numpy as np from scipy.stats import genextreme loc, scale, shape = 0.5, 2, -0.1 # 分布的参数 rv = genextreme(shape, loc, scale) # GEV分布的随机变量生成器 data = rv.rvs(size=1000) # 生成1000个随机样本 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值