pat 1104. Sum of Number Segments

原题链接:https://www.patest.cn/contests/pat-a-practise/1104


1104. Sum of Number Segments (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

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) (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, the size of the sequence which is no more than 105. The next line contains 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



两层for循环区间判断超时了,,然后网上求助,发现是一道类似数学规律的题?!

每个数字i要加的次数是:(n-i)(i+1),,题目就变得简单了。。。。


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    int i,n;
    double x,sum=0.0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lf",&x);
        sum+=x*(n-i)*(i+1);
    }
    printf("%.2f\n",sum);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
这个错误表明,在进行张量相乘时,两个张量的形状不匹配。具体来说,feature_map张量的第1维大小为3,而mask张量的第1维大小为262144。这可能是因为segments张量的形状不正确导致的。你可以尝试将segments张量的形状更改为与img_tensor张量的形状相同,即(1, H, W),其中H和W分别为图像的高度和宽度。可以使用img_tensor的形状信息来创建一个与之相同的全零张量,然后将segments张量的值复制到新张量的正确位置。修改后的代码如下: ``` 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) # 将 segments 张量形状更改为与 img_tensor 相同 new_segments = torch.zeros((1, img_tensor.shape[2], img_tensor.shape[3])) for i in range(segments.shape[0]): for j in range(segments.shape[1]): new_segments[0, i, j] = segments[i, j] segments = new_segments.long() # 可视化超像素索引映射 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, 1) mask = torch.from_numpy(mask).float() # 转换为 PyTorch 张量并进行类型转换 pooled_feature = (feature_map * mask).sum(dim=(2, 3)) / 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() ``` 这个修改应该可以解决这个问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值