#1104. Sum of Number Segments【前缀和】

原题链接

Problem Description:

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) and (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N N N, the size of the sequence which is no more than 1 0 5 10^5 105. The next line contains N N N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:

4
0.1 0.2 0.3 0.4

Sample Output:

5.00

Problem Analysis:

分析样例可知,本题可以先预处理这些数段的前缀和,然后遍历前缀和数组,再找规律求和即可:

for (int i = 1; i <= n; i ++ ) s[i] += s[i - 1] + seq[i];

double res = 0;

for (int i = 0; i < n; i ++ )
	for (int j = i + 1; j <= n; j ++ )
		res += s[j] - s[i];

但由于 N ≤ 1 0 5 N\leq 10^5 N105,这种方法会 TLE,因此考虑优化。

我们可以为 seq 的前缀和数组 s 再维护一个前缀和数组 S,这样只需要预处理一下 S,就可以减少一重循环,在线性复杂度下求得答案。

另外,由于数据原因,这里需要开 long double,其输出格式为 printf("%Lf", res)

Code

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 1e5 + 10;

int n;
long double seq[N];
long double s[N], S[N];

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ ) scanf("%Lf", &seq[i]);
    
    for (int i = 1; i <= n; i ++ ) s[i] = s[i - 1] + seq[i];

    for (int i = 1; i <= n; i ++ ) S[i] = S[i - 1] + s[i];

    long double res = 0;

    for (int i = 0; i < n; i ++ )
    {
        res += S[n] - S[i] - (n - i) * s[i];
    }

    printf("%.2Lf\n", res);
    return 0;
}
import torch import torch.nn.functional as F from skimage.segmentation import slic, mark_boundaries import torchvision.transforms as transforms import numpy as np from PIL import Image import matplotlib.pyplot as plt # 加载图像 image = Image.open('3.jpg') # 转换为 PyTorch 张量 transform = transforms.ToTensor() img_tensor = transform(image).unsqueeze(0) # 将 PyTorch 张量转换为 Numpy 数组 img_np = img_tensor.numpy().transpose(0, 2, 3, 1)[0] # 使用 SLIC 算法生成超像素标记图 segments = slic(img_np, n_segments=60, compactness=10) # 可视化超像素索引映射 plt.imshow(segments, cmap='gray') plt.show() # 将超像素索引映射可视化 segment_img = mark_boundaries(img_np, segments) # 将 Numpy 数组转换为 PIL 图像 segment_img = Image.fromarray((segment_img * 255).astype(np.uint8)) # 保存超像素索引映射可视化 segment_img.save('segment_map.jpg') # 定义超像素池化函数 def superpixel_pooling(feature_map, segments): # 获取超像素数量和特征维度 n_segments = np.unique(segments).size n_channels = feature_map.shape[0] # 初始化超像素特征 pooled_features = torch.zeros((n_segments, n_channels)) # 对每个超像素内的像素特征进行聚合 for segment_id in range(n_segments): mask = (segments == segment_id).reshape(-1, 1, 1) mask = torch.from_numpy(mask).float() # 转换为 PyTorch 张量并进行类型转换 pooled_feature = (feature_map * mask).sum(dim=(1, 2)) / mask.sum() pooled_features[segment_id] = pooled_feature return pooled_features # 进行超像素池化 pooled_features = superpixel_pooling(img_tensor, segments) # 可视化超像素特征图 plt.imshow(pooled_features.transpose(0, 1), cmap='gray') plt.show(),上述代码出现问题:pooled_feature = (feature_map * mask).sum(dim=(1, 2)) / mask.sum() RuntimeError: The size of tensor a (3) must match the size of tensor b (262144) at non-singleton dimension 1,如何 修改
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值