这道题结合了结构体,还巧妙的利用结构体内部的对象记录了DP的前一状态的对象,感觉这种方法可以用在其他DP里,比如重建最长公共子序列。。
今天辛辛苦苦磨磨蹭蹭的做了两道DP,传上来记录一下
#include <iostream> #include <cstring> #include <algorithm> #include <string> #define Max(a,b) a>b?a:b using namespace std; struct Mouse { int id; int weight; int speed; int link; }; bool cmp(Mouse a, Mouse b) { if(a.speed == b.speed) return a.weight > b.weight; else return a.speed > b.speed; } int main() { Mouse mice[1005]; Mouse temp; int m, w, s, n, t, max; int count[1005]; memset(mice, 0, sizeof(mice)); memset(count, 0, sizeof(count)); int i; for(i=1; cin >> w >> s; i++) { temp.id = i; temp.link = -1; temp.weight = w; temp.speed = s; mice[i] = temp; } m = i-1; sort(mice+1, mice+m+1, cmp); for(int i=1; i<=m; i++) //cout << mice[i].id << " " << mice[i].weight << " " << mice[i].speed << endl; max = 1; temp = mice[1]; count[1] = 1; for(int i=2; i<=m; i++) { for(int j=i-1; j>0; j--) { if(mice[j].weight < mice[i].weight) { if(count[i] < count[j]+1) { count[i] = count[j]+1; mice[i].link = j; } } } if(count[i] > max) { max = count[i]; temp = mice[i]; } } int ans[1005] = {0}; int num = 0; while(true) { ans[num] = temp.id; num++; if(temp.link==-1) break; temp = mice[temp.link]; } cout << num << endl; for(int i=num-1; i>=0; i--) cout << ans[i] << endl; return 0; }
2022年9月23日 21:01
Every student of Secondary education can download NCERT STD-10 Urdu Sample Paper 2023 with sample answers to gain high score by knowing the new exam scheme or question paper style for all exams such as SA1, SA2, FA1, FA2, FA3, FA4 and Assignments held under Term-1 & Term-2 of the course. The NCERT have introduced subject wide study & learning material for all regional students of the country. NCERT Urdu Sample Paper Class 10 Every student of Secondary education can download NCERT STD-10 Urdu Sample Paper 2023 with sample answers to gain high score by knowing the new exam scheme or question paper style for all exams such as SA1, SA2, FA1, FA2, FA3, FA4.
2023年7月11日 05:22
Zeus Network provides a dedicated Zeus free trial. Sadly, Zeus does not provide a Zeus free trial period before the membership. The Zeus Network, including documentaries, drama series and reality shows. www.thezeusnetwork.com/activate code firestick Once after downloading, open the application, and “activate Zeus Network” at “thezeusnetwork.com/activate” in search of the available content. Zeus Network without a connecting TV broadcasting service provider the Zeus Network app is available on popular streaming devices.