前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >codeforces 318 A.Even Odds B.Sereja and Array

codeforces 318 A.Even Odds B.Sereja and Array

作者头像
xindoo
发布2021-01-21 18:36:00
2760
发布2021-01-21 18:36:00
举报
文章被收录于专栏:XINDOO的专栏XINDOO的专栏

A.Even Odds

给你n和k, 把从1到n先排奇数后排偶数排成一个新的序列,输出第k个位置的数。

比如 10 3 拍好后就是 1 3 5 7 9 2 4 6 8 10 第3个数是5。

代码语言:javascript
复制
//cf 318 A
//2013-06-18-20.30
#include <iostream>
using namespace std;
int main()
{
    __int64 n, k;
    while (cin >> n >> k)
    {

        if (k <= (n+1)/2)
            cout << (k-1)*2 + 1 << endl;
        else
        {
            k -= (n+1)/2;
            cout << k*2 << endl;
        }
    }
    return 0;
}

B.Sereja and Array

就是找到以“heavy” 开头和“metal”结尾的字符串有多少个。

我的思路是标记“heavy” 和“metal”的位置然后计算以每一个“metal”结尾的有多少个,然后相加。

代码语言:javascript
复制
//cf 318 B
//2013-06-18-21.01
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

const int maxn = 1000006;
char str[maxn];
int s[maxn];
int e[maxn];

int main()
{
    while (scanf("%s", str) != EOF)
    {
        memset(s, 0, sizeof(s));
        memset(e, 0, sizeof(e));
        int l = strlen(str);
        for (int i = 0; i < l-4; i++)
        {
            if (str[i] == 'h' && str[i+1] == 'e' && str[i+2] == 'a' && str[i+3] == 'v' && str[i+4] == 'y')
            {
                s[i] = 1;
                i += 4;
                continue;
            }
            if (str[i] == 'm' && str[i+1] == 'e' && str[i+2] == 't' && str[i+3] == 'a' && str[i+4] == 'l')
            {
                e[i] = 1;
                i += 4;
                continue;
            }
        }
        __int64 ans = 0;
        for (int i = 1; i < l; i++)
        {
            if (e[i])
                ans += s[i-1];
            s[i] += s[i-1];
        }
        cout << ans << endl;
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-06-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档