題目:
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Input
Input 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.
Output
Your 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.
我的代碼
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdio>
#include <stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
#pragma warning(disable : 4996)
using namespace std;
class A
{
public:
long long w;
long long s;
};
int main()
{
long long n, k;
static A shu[10050];
static long long dp[10050][2];
long long op = 1;
while (scanf("%lld %lld", &shu[op].w, &shu[op].s) != EOF)
{
op++;
}
long long yu = 1;
dp[1][0] = 1; dp[1][1] = 0;
for (long long i = 2; i < op; i++)
{
long long maxx = 0;
long long mark = 0;
for (long long j = 1; j < i; j++)
{
if (shu[j].w < shu[i].w&& shu[j].s > shu[i].s)
{
maxx = max(maxx, dp[j][0]);
mark = j;
}
}
if (mark == 0)
{
dp[i][0] = 1;
dp[i][1] = 0;
}
else
{
dp[i][0] = maxx + 1;
dp[i][1] = mark;
}
yu = max(yu, dp[i][0]);
}
cout << yu << endl;
static long long str[10050];
long long jk = 1;
for (long long i = (op - 1); i >= 1; i--)
{
if (dp[i][0] == yu)
{
str[jk] = i; jk++;
while (1)
{
if (i == 0)
{
break;
}
str[jk] = dp[i][1]; jk++;
i = dp[i][1];
}
}
}
for (long long i = (jk - 2); i >= 1; i--)
{
cout << str[i] << endl;
}
}
找不到問題所在,自己嘗試的輸入都沒問題,有沒有大佬幫忙解決一下
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/27640.html
標籤:C++ 語言
上一篇:關于使用pthread_detach后出現Segmentation fault的問題
下一篇:一道C語言的陣列題
