HDU - 1160 FatMouse's Speed
题目:
InputInput contains data for a bunch of mice, one mouse per line, terminated by end of file.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
OutputYour program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[n]]
In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
Sample Input
6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900
Sample Output
4 4 5 9 7
分析:涉及到两个序列的LIS问题,先按照一个序列排序,再对另一个序列用LIS算法即可。
代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<string.h> 4 #include<vector> 5 using namespace std; 6 7 struct Node{ 8 int w,v,i; 9 vector<int> pre; 10 }; 11 12 bool Node_cmp(const Node &n1,const Node &n2){ 13 if(n1.w==n2.w){ 14 return n1.v>n2.v; 15 } 16 return n1.w<n2.w; 17 } 18 19 vector<Node> line; 20 21 int main(){ 22 int index=1,W,V; 23 while(scanf("%d%d",&W,&V)!=EOF){ 24 if(W==0&&V==0){ 25 break; 26 } 27 Node node; 28 node.i=index;index++; 29 node.v=V;node.w=W; 30 line.push_back(node); 31 } 32 sort(line.begin(),line.end(),Node_cmp); 33 //LIS 34 Node ans; 35 for(int i=0;i<line.size();i++){ 36 for(int j=i+1;j<line.size();j++){ 37 if(line[j].v<line[i].v&&line[j].w>line[i].w){ 38 if(line[i].pre.size()+1>=line[j].pre.size()){ 39 line[j].pre=line[i].pre; 40 line[j].pre.push_back(line[i].i); 41 } 42 } 43 } 44 if(line[i].pre.size()>ans.pre.size()){ 45 ans=line[i]; 46 } 47 } 48 if(ans.pre[ans.pre.size()-1]!=ans.i){ 49 ans.pre.push_back(ans.i); 50 } 51 cout <<ans.pre.size()<<endl; 52 for(int i=0;i<ans.pre.size();i++){ 53 cout <<ans.pre.at(i)<<endl; 54 } 55 system("pause"); 56 return 0; 57 }


浙公网安备 33010602011771号