首页 > 其他分享 >[USACO1.5] [IOI1994]数字三角形 Number Triangles

[USACO1.5] [IOI1994]数字三角形 Number Triangles

时间:2024-08-01 10:44:15浏览次数:7  
标签:USACO1.5 int 样例 Number long IOI1994 Triangles

传送锚点

[P1216 USACO1.5] [IOI1994]数字三角形 Number Triangles - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目

[USACO1.5] [IOI1994]数字三角形 Number Triangles

题目描述

观察下面的数字金字塔。

写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大。每一步可以走到左下方的点也可以到达右下方的点。

在上面的样例中,从 \(7 \to 3 \to 8 \to 7 \to 5\) 的路径产生了最大权值。

输入格式

第一个行一个正整数 \(r\) ,表示行的数目。

后面每行为这个数字金字塔特定行包含的整数。

输出格式

单独的一行,包含那个可能得到的最大的和。

样例 #1

样例输入 #1

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

样例输出 #1

30

提示

【数据范围】
对于 \(100\%\) 的数据,\(1\le r \le 1000\),所有输入在 \([0,100]\) 范围内。

题目翻译来自NOCOW。

USACO Training Section 1.5

IOI1994 Day1T1

思路

输入数据时,我们从下标为1开始,然后我们从上往下递推,转移方程为 ans[i][j] = max(ans[i - 1][j], ans[i - 1][j - 1] ) + num[i][j]; 不用担心越界,一开始输入数据时,我们就已经处理了,在输出最大值时,我们要遍历最后一行,

代码

#include <bits/stdc++.h>
typedef long long ll;
const int N = 1e3 + 5;
int num[N][N];
int ans[N][N];
using namespace std;
typedef long long ll;
int main() {
#ifdef ACM_LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= i; j++){
            cin >> num[i][j];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= i; j++){
            ans[i][j] = max(ans[i - 1][j], ans[i - 1][j - 1] ) + num[i][j];
        }
    }
    int ret = 0;
    for(int j = 1; j <= n; j++){
        ret = max(ret, ans[n][j]);
    }
    cout << ret;
    return 0;
}

标签:USACO1.5,int,样例,Number,long,IOI1994,Triangles
From: https://www.cnblogs.com/6Luffy6/p/18336140

相关文章

  • Collecting Numbers II
    原题链接题解首先,对于数字i如果location[i]<location[i-1]那么遍历次数需要+1,否则不变。因此,对于交换的数字x,y而言,他们只能影响x-1,x+1,y-1,y+1的遍历次数,只需要考虑有限的几种情况即可(需要特判abs(x-y)==1的情况)。code #include<bits/stdc++.h>u......
  • Collecting Numbers II
    原题链接题解如果一个\(k\),其前面没有出现过\(k-1\),那么回合数+1,我们令这样的数叫做断点因此交换两个数\(l,r\)不会影响\([1,l-1],[r+1,n]\)内的断点code#include<bits/stdc++.h>#definelllonglong#definelowbit(x)((x)&(-x))usingnamespacestd;constll......
  • SP8099 TABLE - Crash´s number table 题解
    题目传送门前置知识一般的积性函数|数论分块|莫比乌斯反演解法令\(n\lem\)。考虑莫比乌斯反演,推式子,有\(\begin{aligned}&\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\operatorname{lcm}(i,j)\\&=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\frac{ij}{\gcd(i,j)......
  • 题解:[ABC363D] Palindromic Number
    提示特定位数的回文数数量是可以快速计算出来的,对于特定的位数\(n\),第一位由于不能有前导\(0\),共有\(9\)种选择,而从第\(2\)位到第\(\frac{n+1}{2}\)位都有\(10\)种选择(一个回文数完全由它的前半部分确定)。所以\(n\)位数中共有\(S(n)=9*10^\frac{n-1}{2}......
  • [USACO1.5] 八皇后 Checker Challenge 题解
    [USACO1.5]八皇后CheckerChallenge题目描述一个如下的\(6\times6\)的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。上面的布局可以用序列\(2\4\6\1\3\5\)来描述,第\(i\)个数字表示在......
  • 取消pickle错误:magic_number = pickle_module.load(f, **pickle_load_args) _pickle.U
    当我尝试加载.pt文件时,我看到以下错误,str1='Dataset/ALL_feats_cgqa.pt'm=torch.load(str1)错误如下,File"/home/Storage1/pythonCodeArea/train.py",line21,inload_embeddingsm=torch.load(str1)File"/home/.local/lib/python......
  • CF585F Digits of Number Pi 题解
    Description给定长度为\(n\)的数字串\(s\)和长度为\(d\)的不含前导零的数字串\(x,y(x\ley)\)。求存在长度至少为\(\left\lfloor\frac{d}{2}\right\rfloor\)的子串是\(s\)的子串的数字串\(t\in[x,y]\)的数量。\(n\le10^3\),\(d\le50\),答案对\(10^9+7\)取......
  • Leetcode 1334 Find the City With the Smallest Number of Neighbors at a Threshold
    Problem:FindtheCityWiththeSmallestNumberofNeighborsataThresholdDistanceTheknowledgepointsoutsideofmyskilltreeExplanationofCodeandConceptsWhatisfloat('inf')?float('inf')inPythonrepresentspositiveinfin......
  • [LeetCode] 1380. Lucky Numbers in a Matrix
    Givenanmxnmatrixofdistinctnumbers,returnallluckynumbersinthematrixinanyorder.Aluckynumberisanelementofthematrixsuchthatitistheminimumelementinitsrowandmaximuminitscolumn.Example1:Input:matrix=[[3,7,8],[9,11,......
  • buying a SIM card and registering a mobile phone number
    Here’sasampleconversationaboutbuyingaSIMcardandregisteringamobilephonenumber:User:Hi,IneedtobuyaSIMcardandregisteramobilephonenumber.Canyouguidemethroughtheprocess?Salesperson:Ofcourse!First,weneedtochooseamo......