• 东南大学《算法设计基础》课程作业 2


    Question

    Answer

    Code for #2

    #include "pch.h"
    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <memory>
    #include <queue>
    #include <cmath>
    #include <ctime>
    
    using namespace std;
    
    #define NINF -99999999
    #define NMARKET 8
    
    struct ResultNode {
    	int val; // profit
    	queue<int> seq; // operation sequence
    };
    
    // [prevStatus, opCode]
    vector< vector< int > > cb = { {0, 6}, {1, 2}, {0, 3}, // transformed to Have No
    			       {1, 6}, {0, 1}, {1, 4}, {0, 5} }; // transformed to Have
    
    
    int B[NMARKET + 1] = { 0,5,6,7,5,9,7,17,16 };
    int S[NMARKET + 1] = { 0,4,9,10,18,12,6,15,18 };
    
    void gen() {
    	srand(time(NULL));
    	for (int i = 1; i <= NMARKET; i++)
    		B[i] = rand() * 10;
    	for (int i = 1; i <= NMARKET; i++)
    		S[i] = rand() * 5;
    	cout << "init ok" << endl;
    }
    
    int checkResult() {
    	int a[NMARKET + 1][NMARKET + 1] = { 0 };
    	for (int i = 1; i <= NMARKET; i++) {
    		a[i][i] = max(a[i - 1][i - 1] + S[i] - B[i], a[i - 1][i]);
    		for (int j = i + 1; j <= NMARKET; j++) {
    			a[i][j] = max(max(a[i][i] + S[j] - B[i], a[i - 1][j]), a[i][j - 1]);
    		}
    	}
    	return a[NMARKET][NMARKET];
    }
    
    int F(int i, int j) {
    	int r = 0;
    	switch (j) {
    	case 1:
    		r -= B[i]; break;
    	case 2:
    		r += S[i]; break;
    	case 3:
    		r -= B[i]; r += S[i]; break;
    	case 4:
    		r += S[i]; r -= B[i]; break;
    	case 5:
    		r -= B[i]; r += S[i]; r -= B[i]; break;
    	case 6:
    		r = 0;
    	}
    	return r;
    }
    
    void printSeq(queue<int> res) {
    	int i = 1;
    	while (!res.empty()) {
    		switch (res.front()) {
    		case 1: cout << "(" << i << ","; break;
    		case 2: cout << i << "),"; break;
    		case 3: cout << "(" << i << "," << i << "),"; break;
    		case 4: cout << i << "),(" << i << ","; break;
    		case 5: cout << "(" << i << "," << i << "),(" << i << ","; break;
    		case 6: break;
    		}
    		i++;
    		res.pop();
    	}
    	cout << endl;
    }
    
    int main() {
    	// gen();
    
    
    	ResultNode dp[NMARKET + 1][2];
    
    	dp[0][0].val = 0;
    	dp[0][1].val = NINF;
    
    	for (int i = 1; i <= NMARKET; i++) {
    		// xxx -> have no
    		for (auto j = cb.begin(); j < cb.begin() + 3; j++) {
    			if (dp[i - 1][(*j)[0]].val + F(i, (*j)[1]) > dp[i][0].val) {
    				dp[i][0].val = dp[i - 1][(*j)[0]].val + F(i, (*j)[1]);
    				dp[i][0].seq = queue<int>(dp[i - 1][(*j)[0]].seq);
    				dp[i][0].seq.push((*j)[1]);
    			}
    		}
    		// xxx -> have
    		for (auto j = cb.begin() + 3; j < cb.end(); j++) {
    			if (dp[i - 1][(*j)[0]].val + F(i, (*j)[1]) > dp[i][1].val) {
    				dp[i][1].val = dp[i - 1][(*j)[0]].val + F(i, (*j)[1]);
    				dp[i][1].seq = queue<int>(dp[i - 1][(*j)[0]].seq);
    				dp[i][1].seq.push((*j)[1]);
    			}
    		}
    	}
    
    	cout << "My max profit is: " << endl;
    	cout << dp[NMARKET][0].val << endl;
    	
    	cout << "The translated operation sequence is: " << endl;
    	printSeq(dp[NMARKET][0].seq);
    	cout << "The original operation sequence is: " << endl;
    	while (!dp[NMARKET][0].seq.empty()) {
    		cout << dp[NMARKET][0].seq.front() << " ";
    		dp[NMARKET][0].seq.pop();
    	}
    	cout << endl;
    
    	cout << "The DP array is " << endl;
    	for (int i = 0; i <= NMARKET; i++) {
    		for (int j = 0; j <= 1; j++)
    			cout << dp[i][j].val << " ";
    		cout << endl;
    	}
    	cout << endl;
    	cout << "Your max profit is:" << endl;
    	cout << checkResult() << endl;
    
    	system("pause");
    	return 0;
    
    }
    
    
  • 相关阅读:
    JavaScript npm/nrm 切换安装依赖的镜像源
    Vue Vuex中的严格模式/实例解析/dispatch/commit /state/getters
    Vuex mapGetter的基本使用
    Springboot 整合Mybatis-plus
    SEO基本功:站内优化的一些基本手段
    解决使用logstash中jdbc导入mysql中的数据到elasticsearch中tinyint类型被转成布尔型的问题的方法
    【重大好消息】elasticsearch 7.3版本已经可以免费使用x-pack就可以设置账号和密码了,让你的数据不再裸奔
    elasticsearch7.3版本已经不需要额外安装中文分词插件了
    网络案例分析之999皮炎平出鹤顶红色号的口红
    php框架symfony踩坑苦旅(1)
  • 原文地址:https://www.cnblogs.com/zxuuu/p/14236363.html
Copyright © 2020-2023  润新知