codeforces 318 A.Even Odds B.Sereja and Array

简介: codeforces 318 A.Even Odds B.Sereja and Array

A.Even Odds  


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


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


//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”结尾的有多少个,然后相加。


//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;
}
目录
相关文章
|
7月前
|
人工智能
codeforces 315 B.Sereja and Array
codeforces 315 B.Sereja and Array
19 0
|
7月前
codeforces 299 A. Ksusha and Array
题目就是让你找出一个数组中可以将这个数组中所有数整除的数,很明显,如果存在,这个数肯定是最小的一个。
24 0
|
11月前
|
存储
P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
codeforces1426——F. Number of Subsequences(DP)
codeforces1426——F. Number of Subsequences(DP)
86 0
|
Python
[Leetcode][python]Maximum Subarray/最大子序和
题目大意 由 N 个整数元素组成的一维数组 (A[0], A[1],…,A[n-1], A[n]),这个数组有很多连续子数组,那么其中数组之和的最大值是什么呢? 子数组必须是连续的。 不需要返回子数组的具体位置。 数组中包含:正整数,零,负整数。
81 0
|
Java 数据安全/隐私保护
[LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数。 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ] 中有 3 个岛。
1226 0
|
算法
洛谷 P1348 Couple number
题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number。你的工作就是判断一个数N是不是Couple number。 输入输出格式 输入格式: 仅一行,两个长整型范围内的整数$n_1$和$n_2$,之间用1个空格隔开。
1070 0