CF Gym 100500H ICPC Quest

题意:给一个nXm的矩阵,上面有一些数字,从左上角出发,每次只能往右或者往下,把沿途的数字加起来,求到达右下角的最大值是多少。

题解:简单的一个dp,设f[i][j]为到达i行j列的最大值,f[i][j] = max(f[i-1][j],f[i][j-1])+a[i][j],然后用队列刷个表就行了。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
//#define local
#define fi first
#define se second
const int maxn = 1005;

int f[maxn][maxn];
int a[maxn][maxn];
int main()
{
    int T;
    scanf("%d",&T);
    for(int k = 1; k <= T; k++) {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                scanf("%d",a[i]+j);
            }
        }
        queue< pair<int,int> > q;
        memset(f,0x80,sizeof(f));
        q.push(make_pair(0,0));
        f[0][0] = a[0][0];
        while(q.size()){
            pair<int,int> &u = q.front();
            int r = u.fi, c = u.se;
            if(r<n && f[r+1][c]<f[r][c]+a[r+1][c]) f[r+1][c] = f[r][c]+a[r+1][c],q.push(make_pair(r+1,c));
            if(c<m && f[r][c+1]<f[r][c]+a[r][c+1]) f[r][c+1] = f[r][c]+a[r][c+1],q.push(make_pair(r,c+1));
            q.pop();
        }
        printf("Case %d: %d\n",k,f[n-1][m-1]);
    }
    return 0;
}
时间: 2024-08-09 04:12:09

CF Gym 100500H ICPC Quest的相关文章

CF Gym 100500C ICPC Giveaways

读懂题意就是水题,按照出现次数对下标排一下序,暴力.. #include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; const int maxn = 1e4+77; ll cnt[maxn]; ll r[maxn]; bool cmp(int a,int b) { return cnt[a] > cnt[b]; } int mai

codeforces Gym 100500H A. Potion of Immortality 简单DP

Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description Noura Boubou is a Syrian volunteer at ACM ACPC (Arab Collegiate Programming Contest) since 2011. She graduated from Tishreen Un

CF gym 101933 K King&#39;s Colors —— 二项式反演

题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \),设为 \( f(i) \),设恰好 i 种颜色为 \( g(i) \) 那么 \( f(i) = \sum\limits_{j=0}^{i} C_{i}^{j} * g(j) \) 二项式反演得到 \( g(i) = \sum\limits_{j=0}^{k} (-1)^{k-j} * C_{k}

CF Gym 101955G Best ACMer Solves the Hardest Problem

链接:https://codeforces.com/gym/101955/problem/G 题意:在二维平面上四种操作: 1,加一个带权的点: 2,删去一个点: 3,给一个点周围欧几里得距离为sqrt(k)的存在的点点权都加w: 4,查询一个到点欧几里得距离为sqrtk的点权和. x, y<6000, k<1e7, sigma(询问次数)<1e6,time:12s 题解:原本以为是数据结构,发现距离为k的x,y其实不多,直接存vector<pii>dis[maxk]暴力即可

CF Gym 102059E Electronic Circuit (set存图删点)

链接:https://codeforces.com/gym/102059/problem/E 题意:n个点, m条线,问这个电路是否合法,合法:可以确定一个起点和一个终点. 题解:不断的删点,删除度数为2的点,再相连,看最终度数为1的点的个数是否为2.set存图 #include <bits/stdc++.h> using namespace std; const int maxn=3e5+5; set<int> S[maxn]; int n, m; void del_edge()

cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

题目: Description standard input/output As most of you know, the Arab Academy for Science and Technology and Maritime Transport in Alexandria, Egypt, hosts the ACPC Headquarters in the Regional Informatics Center (RIC), and it has been supporting our r

cf gym 100960 G. Youngling Tournament set+树状数组

G. Youngling Tournament time limit per test 2 seconds memory limit per test 256 mebibytes input standard input output standard output Yoda, the Grand Master of the Jedi Order, hit on the idea to hold a tournament among younglings. He has not chosen t

CF Gym 100187A Potion of Immortality

根据兔子试药情况可以缩小范围,如果死了,不在试过的药里面,如果活着,在试过的药里. 最糟的情况: 两个原则 1.能确定药所在的范围的尽量大,2.死得兔子尽量多. 如果当前不知道情况的药n为k的二倍以上,那么基于上面两个原则,试过药的兔子肯定会死. 没死:范围k,损失的兔子0 死了:范围n-k,损失的兔子1 (n>2*k) 设r=n%k,经过上述过程,损失了n/k-1只兔子,转移到了当前状态范围w = k+r, 1.r == 0 那么可以补充一个毒药,变成w=k+1,根据鸽巢原理再死一个就可以确定

CF Gym 100500A Poetry Challenge

题解:暴力dfs,如果有一个选择,能让对手必败,那么就是必胜态,能转移到的状态都是对手的必胜态,或者无法转移,就是必败态. 总算是过了,TLE是因为状态没判重,不过我还是有一点没想明白,为什么状态会出现重复 #include<cstdio> #include<cmath> #include<vector> #include<map> #include<set> #include<algorithm> #include<iostr