2015暑假第二次积分赛

本文解析了两道经典的编程题目,第一题通过动态规划解决数列分段问题,第二题利用哈希表统计字符串出现次数,实现高效匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址

Cent Savings

题目大意:给出数n,d及n个数,将n个数按照给出顺序最多分成d+1段,单段之和进行个位四舍五入,求操作后的最小和

解题思路:跟01背包类似,DP[i][j]表示将前i个数分成j+1段的最小和,再加一个内层循环枚举一下最后一段的位置

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <list>
#include <set>

using namespace std;

const int maxn = 2000+100;
const int INF = 0xfffffff;

int a[maxn],dp[maxn][30];

int main()
{
    int n,d;
    while(scanf("%d%d",&n,&d) != EOF)
    {
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d", &x);
            a[i] = a[i-1]+x;
        }
        for(int i = 1; i <= n; i++)
            for(int j = 0; j <= n; j++)
                dp[i][j] = INF;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j <= d && j < i; j++)
            {
                if(!j)
                {
                    int x = a[i];
                    if(x%10<5) x -= x%10;
                    else x = (x/10+1)*10;
                    dp[i][j] = x;
                }
                else
                {
                    for(int k = 1; k < i; k++)
                    {
                        int x = dp[k][j-1] + a[i] - a[k];
                        if(x%10<5) x -= x%10;
                        else x = (x/10+1)*10;
                        dp[i][j] = min(dp[i][j], x);
                    }
                }
            }
        }
        int ans = INF;
        for(int i = 0; i < n && i <= d; i++)
            ans = min(ans, dp[n][i]);
        printf("%d\n", ans);
    }
    return 0;
}

Judging Troubles

题目大意:给出n及n个字符串,再n个字符串,求这2次n个字符串中相同字符串的个数

解题思路:用map,第一个键值表示字符串,第二个键值表示个数,再用迭代器分别访问每个字符串的个数,注意map里的string可以是char,输入别用cin,因为这个超时n次

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>

using namespace std;

int main()
{
    int n;
    int c1,c2;
    char s[50];
    map<string,int> cnt1,cnt2;
    map<string,int>::iterator it1,it2;
    while(scanf("%d",&n) != EOF)
    {
        cnt1.clear();
        cnt2.clear();
        getchar();
        for(int i = 0; i < n; i++)
        {
            scanf("%s",s);
            cnt1[s]++;
        }
        for(int i = 0; i < n; i++)
        {
            scanf("%s",s);
            cnt2[s]++;
        }
        int cnt = 0;
        for(it1 = cnt1.begin(); it1 != cnt1.end(); it1++)
        {
            it2 = cnt2.find(it1->first);
            if(it2 != cnt2.end())
                cnt += min(it1->second,it2->second);
        }
        printf("%d\n",cnt);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值