Description
Input
Output
Sample Input
Sample Output
#include
#include
#include
#include
#include
using namespace std;
#define N 1000010
int n;
int tt[N];
int main()
{
int t;
while(~scanf("%d",&n))
{
memset(tt, 0, sizeof(tt));
int ans = 0;
for(int i = 0; i < n; i ++)
{
scanf("%d",&t);
tt[t] ++;
if(tt[t] >= (n + 1)/2)
ans = max(ans, t);
}
printf("%d\n", ans);
}
}
Description
Input
Output
Sample Input
Sample Output
#include
#include
#include
#include
#include
using namespace std;
#define N 1010
int tt[N];
int dp[N];
int main()
{
int n;
while(~scanf("%d",&n), n)
{
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i ++)
{
scanf("%d",&tt[i]);
}
dp[0] = tt[0];
int ans = tt[0];
for(int i = 1; i < n; i ++)
{
dp[i] = tt[i];
for(int j = 0; j < i; j ++)
{
if(tt[j] < tt[i])
{
dp[i] = max(dp[i],dp[j] + tt[i]);
}
}
if(ans < dp[i])
ans = dp[i];
}
printf("%d\n", ans);
}
}
Description
Input
Output
Sample Input
Sample Output
恰好装满的背包问题,最后判断一下就可以了
#include
const int N = 550;
int c[N];
int w[N];
int dp[10010];
const int INF = 10000000;
int min(int x, int y)
{
return x < y ? x : y ;
}
int main()
{
int n, m;
int i, j;
int a, b;
int cc, ww;
while(~scanf("%d",&n))
{
while(n --)
{
scanf("%d%d", &cc, &ww);
scanf("%d", &m);
for(i = 0; i <= ww - cc; i ++)
dp[i] = INF;
for(i = 0; i < m; i ++)
scanf("%d%d",&c[i], &w[i]);
dp[0] = 0;
for(i = 0;i < m; i ++)
{
for(j = w[i]; j <= ww - cc; j ++)
dp[j] = min(dp[j - w[i]] + c[i], dp[j]);
}
if(dp[ww - cc] == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[ww - cc]);
}
}
return 0;
}
Description
Input
Output
Sample Input
Sample Output
可以把它转换成数塔问题,一开始我从上往下,绕了许久!在贪心的时候,也要注意范围!!!
#include
#include
#include
#include
#include
using namespace std;
#define N 100010
int dp[N][12];
int main()
{
int n;
int a, t;
while(~scanf("%d",&n), n)
{
memset(dp, 0, sizeof(dp));
int maxt = 0;
for(int i = 1; i <= n; i ++)
{
scanf("%d%d",&a,&t);
maxt = max(maxt, t);
dp[t][a + 1] ++;
}
for(int i = maxt - 1; i >= 0; i --)
{
for(int j = 1; j <= 11; j ++)
{
dp[i][j] += max(dp[i + 1][j], max(dp[i + 1][j + 1], dp[i + 1][j -1]));
}
}
printf("%d\n", dp[0][6]);
}
return 0;
}
Description
Input
Output
Sample Input
Sample Output
这道题想了很久,最后用贪心写的,网上有许多不同的解法,不知道是数据弱还是怎么回事。
用一个数组记录最大值,如果比它大,再取一个,到最后有几个,就是答案。
#include
#include
#include
#include
using namespace std;
#define N 120
#define Max 1 << 29
int tt[N];
int ans[N];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 1; i <= n; i ++)
{
scanf("%d",&tt[i]);
}
int tmp_1 = 1;
int cnt = 1;
ans[1] = tt[1];
for(int i = 2; i <= n; i ++)
{
bool flag = false;
for(int j = 1; j <= cnt; j ++)
{
if(ans[j] > tt[i])
{
ans[j] = tt[i];
flag = true;
break;
}
}
if(!flag)
{
ans[cnt + 1] = tt[i];
cnt ++;
//tmp_1 ++;
}
}
printf("%d\n", cnt);
}
}
Description
Input
Output
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
这题就是最长公共子序列了、
注意边界的问题!
#include
#include
#include
#include
#include
using namespace std;
#define N 1001
#define Max 1 << 29
char str1[N];
char str2[N];
int dp[N][N];
int main()
{
while(~scanf("%s%s",str1 ,str2 ))
{
int len1 = strlen(str1);
int len2 = strlen(str2);
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= len1; i ++)
{
for(int j = 1; j <= len2; j ++)
{
//dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if(str1[i - 1] == str2[j - 1])
{
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else
{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
/*for(int i = 0; i < len1; i ++)
{
for(int j = 0; j < len2 ; j ++)
{
printf("%d", dp[i][j]);
}
printf("\n");
}*/
printf("%d\n", dp[len1][len2]);
}
}
/******
abcfbc abfcab
programming contest
abcd mnp
aaaaa a
aaa aaaaaa
************/
Description
Input
Output
Sample Input
7 1 7 3 5 9 4 8
Sample Output
4
最长上升序列
#include
#include
#include
#include
using namespace std;
#define N 1010
int tt[N];
int slen[N];
void init(int n)
{
for(int i = 1; i <= n; i ++)
{
slen[i] = 1;
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init(n);
for(int i = 1; i <= n; i ++)
{
scanf("%d",&tt[i]);
}
int ans = 1;
for(int i = 2; i <= n; i ++)
{
int maxn = 0;
for(int j = 1; j <= i - 1; j ++)
{
if(tt[j] < tt[i] && slen[j] > maxn)
maxn = slen[j];
}
slen[i] = maxn + 1;
if(slen[i] > ans)
ans = slen[i];
}
printf("%d\n",ans);
}
}