CSDN31周赛

感觉T4数据有问题。希望出题可以拍一拍

第一题是很典的股票问题

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0;
        int cur = 0;
        for(int i = 0; i < prices.size(); i++){
            if(prices[cur] < prices[i]){
                res = max(res, prices[i] - prices[cur]);
                swap(prices[cur], prices[i]);
            }
            cur = i;
        }
        return res;
    }
};

第二题也是很典的矩阵DP问题

#include<bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rev(a) reverse((a).begin(), (a).end())
#define each(x, a) for(auto& x : a)
#define mst(a, x) memset(a, x, sizeof(a))
#define rep(i, from, to) for(int i = from;i<to;i++)
#define rrep(i, from, to) for(i128 i = from;i>=to;i--)
#define to_uni(a) a.erase(unique(begin(a), end(a)), end(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl "\n"
#define i128 __int128
#define ll long long
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1};
const int fx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, fy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int N = 1e5 + 10;
long long dp[1005][1005];
void solve() {
	int m, n;
	cin >> m;
	n = m;
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> dp[i][j];
		}
	}
	for (int i = 0; i <= n + 1; i++) {
		dp[0][i] = 9999999;
		dp[m + 1][i] = 9999999;
	}
	for (int i = 0; i <= m + 1; i++) {
		dp[i][0] = 9999999;
		dp[i][n + 1] = 9999999;
		}
		for (int i = 1; i <= m; i++) {
			for (int j = 1; j <= n; j++) {
			if (i == 1 && j == 1) {
				continue;
			} else {
				dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]);
			}
		}
	}
	cout << dp[m][n];
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T = 1;
	// cin >> T;
	while (T--) {
	solve();
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值