Today's Topic
1. Greed Exercise - Three Kingdoms Game Problem
Analysis: Because the best combination is absolutely impossible to obtain, but because people take first, then the second largest one can be obtained, so we find out the second largest one in each combination, and take the largest one in the second largest first, then people will win, and the maximum tacit understanding value is also the value.
Code:
#include<iostream> #include<string> #include<cstring> #include<memory> #include<set> #include<map> #include<vector> #include<stack> #include<queue> #include<functional> using namespace std; int main() { int n; int a[510][510]; memset(a,0,sizeof(a)); cin>>n; for(int i = 0;i<n-1;++i) { for(int j = i+1;j<n;++j) { cin>>a[i][j]; a[j][i] = a[i][j]; } } int ans = 0; int first = 0; int second = 0; for(int i = 0;i<n;++i) { first = 0; second = 0; for(int j = 0;j<n;++j) { if(a[i][j]>first) { second = first; first = a[i][j]; } else if(a[i][j]>second) second = a[i][j]; } //cout<<"first"<<first<<endl; //cout<<"second"<<second<<endl; ans = max(ans,second); } cout<<1<<endl; cout<<ans<<endl; return 0; }
2. Greed Problem - The Use of Huffman Code
Title: In an orchard, most of the fruits have been beaten down, and they are divided into different heaps according to different kinds of fruits. Dodo decided to put all the fruits together.
Every merger, more than two piles of fruit can be merged together, the physical expenditure is equal to the sum of the weight of two piles of fruit. It can be seen that after n-1 mergers of all the fruits, there is only one pile left. The total amount of physical energy consumed in merging fruits is equal to the sum of physical energy consumed in each merger.
Because it takes a lot of effort to move these fruits home, it is necessary to save as much energy as possible when combining the fruits. Assuming that the weight of each fruit is 1, and the number of types and fruits of each fruit is known, your task is to design a merged sequencing scheme to minimize the amount of physical energy consumed and output the minimum value of physical energy expenditure.
For example, there are three kinds of fruits, the number of which is 1, 2, 9 in turn. First, 1 and 2 heaps can be merged, the number of new heaps is 3, and the energy consumption is 3. Then, the new reactor is merged with the original third reactor, and a new reactor is obtained, the number of which is 12 and the physical cost is 12. So the total physical cost is 3 + 12 = 15. It can be proved that 15 is the minimum physical cost.
Code:
#include<iostream> #include<string> #include<cstring> #include<memory> #include<set> #include<map> #include<vector> #include<stack> #include<queue> #include<functional> using namespace std; //Application of Huffman Coding Tree //Use small root heaps, i.e. minimize the repetition of large weights int main() { priority_queue<int,vector<int>, greater<int> >q; int n; int num; cin>>n; for(int i = 0;i<n;++i) { cin>>num; q.push(num); } int res = 0; while(q.size()>1) { int num1 = q.top(); q.pop(); int num2 = q.top(); q.pop(); q.push(num1+num2); res += num1+num2; } cout<<res<<endl; return 0; }
3. Greed-the Salesman Problem
Title:
Amin is a salesman. He was ordered to sell his company's products on Screw Street. Screw Street is a dead end. The exit is the same as the entrance. On one side of the street is the fence, and on the other side is the residents. There are N households in Screw Street. The distance from the first household to the entrance is Sim. Since there may be more than one household in the same house, there may be more than one household at the same distance from the entrance. Amin will enter from the entrance, sell products to X households on Screw Street in turn, and then go out the same way.
Amin accumulates 1 point fatigue value for every 1 metre he walks and accumulates Ai point fatigue value for selling products to the first household. Amin is a workaholic. He wants to know how many points of fatigue he can accumulate for different X without taking the extra road.
Train of thought: Take the present price which is the most expensive, and then the cost is based on this point. If it's on the right, the cost is 2* distance difference + fatigue value, and if it's on the left, add the fatigue value directly.
Use the priority queue to pop up the minimum cost.
Code:
#include<iostream> #include<string> #include<cstring> #include<memory> #include<set> #include<map> #include<vector> #include<stack> #include<queue> #include<functional> using namespace std; //Household structure struct node { int dis; int val; } ; //greedy int main() { node a[110000]; node tmp; int n; cin>>n; for(int i = 1;i<=n;++i) cin>>a[i].dis; for(int i = 1;i<=n;++i) cin>>a[i].val; //x from 1 to n priority_queue<int> q; int from = 0; int cur = 0; int max = 0; int p; q.push(0); a[0].dis = 0; a[0].val = 0; int res = 0; for(int i = 0;i<n;++i) { //It doesn't have to be bigger each time, so it's initialized to 0 first. p = 0; max = q.top(); //Whether there is more depends on the previous decision. for(int j = cur+1;j<=n;++j) { int num = 2*(a[j].dis-a[cur].dis)+a[j].val; if(num>max) { max = num; p = j; } } //There are bigger ones. //But the roundtrip path only needs to be added once through p. //So after that, you just need to add the fatigue value of a single point. if(p) { from = cur; cur = p; q.push(max); for(int j = from+1;j<cur;++j) { q.push(a[j].val); } } res+=q.top(); q.pop(); cout<<res<<endl; } return 0; }
Xiao Zhan!!!