WA主厨和数字 [英] WA on Chef and Digits

查看:112
本文介绍了WA主厨和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助CodeChef的Chef和Digits的代码 http://www.codechef.com/APRIL14/problems / ADIGIT / [ ^ ]



我的代码以某种方式给了WA,我尝试了每一个我想到的测试用例并且不知道出了什么问题。有人能看到问题吗?



算法是:



1.排序一份副本M矢量输入中给出的索引x

2.扫过字符串,计算出每个数字的出现次数。每当我点击其中一个x时,我会从当前计数中计算出答案并将答案放在一个由x索引的数组中。

3.通过循环原始列表以正确的顺序输出答案这是我的代码:



I need help with my code for CodeChef's Chef and Digits http://www.codechef.com/APRIL14/problems/ADIGIT/[^]

My code somehow gives WA, I tried every test case that came to my mind and don't know what is wrong. Can someone see the problem?

Algorithm is:

1. Sort a copy of the list of indices x given in the input in M vector.
2. Sweep through the string, counting the occurrences of each digit as I go along. Each time I hit one of the x, I compute the answer from the current counts and put the answer in a array indexed by x.
3.Output the answers in correct order by looping through the original list and looking up the value in answers massive.
Here is my code:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include<iostream>
#include<vector>
#include<fstream>
#include<algorithm>
#include<string>
#include<cmath>
#include<sstream>
using namespace std;
int n, m, t;
string temp;
int counter[10];
int answers[100002];

int answer(int mas[], int x) {
    int B = 0;
    for (int i = 0; i <= 9; i++)
        B += mas[i] * abs(x - i);
    return B;
}



int main() {
    cin>>n>>m;
    vector<int>M;
    vector<int>M2;
    cin >> temp;
    int B = 0;
    for (int i = 0; i < m; i++) {
        cin>>t;
        M.push_back(t);
        M2.push_back(t);
    }
    sort(M.begin(), M.end());

    int k = 0;

    for (int i = 1; i <= n; i++)
    {
        if (k == M.size()) break;
        if (i == M[k])
        {
            answers[M[k]] = answer(counter, temp[M[k]-1]-'0');
            k++;
        }
        counter[temp[i - 1] - '0']++;
    }

    for (int i = 0; i < m; i++)
        printf("%d\n", answers[M2[i]]);

    return 0;
}

推荐答案

这篇关于WA主厨和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆