这是一段 我怎么提交都WA的 代码
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
typedef struct{
int speed;
int weight;
int num;
}m;
bool cmp(m a,m b)
{
if(a.weight==b.weight) return a.speed>b.speed;
return a.weight<b.weight;
}
typedef struct{
int last;
int total;
}e;
int main()
{
int n=0;
/*cin>>n;
for(int i=1;i<=n;i++){
cin>>mice[i].weight>>mice[i].speed;
mice[i].num=i;
}*/
m mice[100005];
e f[10005];
while(cin>>mice[++n].weight>>mice[n].speed)//++写在这里 不对,我猜测
{
mice[n].num=n;
//cout<<n<<endl;
//n++;
}
n++;
sort(mice+1,mice+n,cmp);
//问题转换成了一个最长下降子序列;
for(int i=1;i<n;i++){
f[i].last=0;
f[i].total=1;
}
for(int i=1;i<n;i++){
for(int j=1;j<i;j++){
if(mice[i].speed<mice[j].speed&&mice[i].weight>mice[j].weight){
if(f[i].total<(f[j].total+1)){
f[i].total=f[j].total+1;
f[i].last=j;
}
}
}
}
int maxv=0;
int maxx=0;
for(int i=1;i<n;i++){
if(f[i].total>maxx){
maxx=f[i].total;
maxv=i;
}
}
cout<<maxx<<endl;
//cout<<maxv<<endl;
int xh[10005];
for(int j=0;j<maxx;j++){
xh[j]=maxv;
//cout<<xh[j]<<" ";
maxv=f[maxv].last;
}
//cout<<endl;
for(int j=maxx-1;j>=0;j--){
cout<<mice[xh[j]].num<<endl;
}
return 0;
}
下面是AC代码 不同的是++写在函数体cin外面
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
typedef struct{
int speed;
int weight;
int num;
}m;
bool cmp(m a,m b)
{
if(a.weight==b.weight) return a.speed>b.speed;
return a.weight<b.weight;
}
typedef struct{
int last;
int total;
}e;
int main()
{
int n=1;
/*cin>>n;
for(int i=1;i<=n;i++){
cin>>mice[i].weight>>mice[i].speed;
mice[i].num=i;
}*/
m mice[100005];
e f[10005];
while(cin>>mice[n].weight>>mice[n].speed)
{
mice[n].num=n;
//cout<<n<<endl;
n++;
}
sort(mice+1,mice+n+1,cmp);
//问题转换成了一个最长下降子序列;
for(int i=1;i<n;i++){
f[i].last=0;
f[i].total=1;
}
for(int i=1;i<n;i++){
for(int j=1;j<i;j++){
if(mice[i].speed<mice[j].speed&&mice[i].weight>mice[j].weight){
if(f[i].total<(f[j].total+1)){
f[i].total=f[j].total+1;
f[i].last=j;
}
}
}
}
int maxv=0;
int maxx=0;
for(int i=1;i<n;i++){
if(f[i].total>maxx){
maxx=f[i].total;
maxv=i;
}
}
cout<<maxx<<endl;
//cout<<maxv<<endl;
int xh[10005];
for(int j=0;j<maxx;j++){
xh[j]=maxv;
//cout<<xh[j]<<" ";
maxv=f[maxv].last;
}
//cout<<endl;
for(int j=maxx-1;j>=0;j--){
cout<<mice[xh[j]].num<<endl;
}
return 0;
}
完全想不通2份代码出了++n还有那里不一样