Codeforces-Round 174(Cows and Sequence)

这道题是树状数组的题,但是用普通数组也能整出来,没学树状数组,就用的普通数组,算是高效算法吧,下面是我的修改思路:

1.一上来我写了如下代码:把每个新加进去的数压入vector,但是我在当t=1时,我的想法是将v[i]一个一个加上去,这样肯定会超时,而且我这个方法的sum是最后一起求的,这样多了个循环,又耗费时间。

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(0);
    int n;cin>>n;
    while(n--)
    {
        int t;cin>>t;
        if(t == 1)
        {
            int a,x;cin>>a>>x;
            for(int i = 0;i < a;i++)
                v[i] += x;
        }
        if(t == 2)
        {
            int k;cin>>k;
            v.push_back(k);
        }
        if(t == 3)
            v.pop_back();
        int sum = 0;
        for(int i = 0;i < v.size();i++)
            sum += v[i];
        printf("%.6lf\n",(double)sum*1.0/v.size());
    }
    return 0;
}

2.想了一下,做出以下修改:虽然我意识到了sum用成全局的,随输随加,但其余的真是纯属脑残,因为我没考虑到连续把最后一个数删除的情况,看来输进去的数还是要用vector保存

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int n,last = 0,shu = 1,a,k,x,sum = 0;cin>>n;
    while(n--)
    {
        int t;cin>>t;
        if(t == 1) {cin>>a>>x;sum += a*x;}
        if(t == 2) {cin>>k;sum += k;last = k;shu++;}
        if(t == 3) {sum -= k;shu--;}
        printf("%.6lf\n",sum*1.0/shu);
    }
    return 0;
}

3.又修改:这样去除了sum的循环,但是当t=1时循环还在,还TLE

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(0);
    int n,last = 0,shu = 1,a,k,x,sum = 0;cin>>n;
    while(n--)
    {
        int t;cin>>t;
        if(t == 1)
            {
                cin>>a>>x;sum += a*x;
                for(int i = 0;i < a;i++)
                    v[i] += x;
            }
        if(t == 2)
            {cin>>k;sum += k;v.push_back(k);}
        if(t == 3)
            {sum -= v[v.size() - 1];v.pop_back();}
        printf("%.6lf\n",(double)sum*1.0/v.size());
    }
    return 0;
}

4.无奈之下开始打表,把每个数的位置对应的加的数都打到表里,无奈有挂了,还是WA

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(0);
    int n,last = 0,shu = 1,k,a,x,sum = 0,biao[200005];cin>>n;
    memset(biao,0,sizeof(biao));
    while(n--)
    {
        int t;cin>>t;
        if(t == 1)
        {
            cin>>a>>x;
            for(int i = 0;i < a;i++)
                biao[i] += x;
            sum += a*x;
        }
        if(t == 2)
            {cin>>k;sum += k;v.push_back(k);}
        if(t == 3)
            {
                sum -= v[v.size() - 1];
                sum -= biao[v.size() - 1];
                biao[v.size() - 1] = 0;
                v.pop_back();
            }
        printf("%.6lf\n",(double)sum*1.0/v.size());
    }
    return 0;
}

5.经过参考网上的题解,我发现这题竟然用long long!就修改成long long

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(0);
    int n,last = 0,shu = 1,k,a,x,biao[200005];cin>>n;
    long long sum = 0;
    memset(biao,0,sizeof(biao));
    while(n--)
    {
        int t;cin>>t;
        if(t == 1)
        {
            cin>>a>>x;
            for(int i = 0;i < a;i++)
                biao[i] += x;
            sum += a*x;
        }
        if(t == 2)
            {cin>>k;sum += k;v.push_back(k);}
        if(t == 3)
            {
                sum -= v[v.size() - 1];
                sum -= biao[v.size() - 1];
                biao[v.size() - 1] = 0;
                v.pop_back();
            }
        printf("%.6lf\n",(double)sum*1.0/v.size());
    }
    return 0;
}

6.修改后又挂了,TLE,发现这里有点小技巧,t=1时根本不用将biao数组的所有前a个值都加x,将a加上x即可,然后从t=3里面调节,太巧妙了,终于AC

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v;
    v.push_back(0);
    int n,last = 0,shu = 1,k,a,x,biao[200005];cin>>n;
    long long sum = 0;
    while(n--)
    {
        int t;cin>>t;
        if(t == 1)
        {
            cin>>a>>x;
            biao[a - 1] += x;
            sum += a*x;
        }
        if(t == 2)
            {cin>>k;sum += k;v.push_back(k);}
        if(t == 3)
            {
                sum -= v[v.size() - 1];
                sum -= biao[v.size() - 1];
                biao[v.size() - 2] += biao[v.size() - 1];
                biao[v.size() - 1] = 0;
                v.pop_back();
            }
        printf("%.6lf\n",(double)sum*1.0/v.size());
    }
    return 0;
}

Codeforces-Round 174(Cows and Sequence)

时间: 2024-11-15 17:57:10

Codeforces-Round 174(Cows and Sequence)的相关文章

Codeforces Round #174 (Div. 2)---C. Cows and Sequence(操作序列)

Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of th

Codeforces Round #174 (Div. 2)---D. Cow Program(dp, 记忆化搜索)

Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1,?a2,?-,?an of positive integers: Initially, x?=?1 and y?=?0. If, after any step, x

Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号个数比右括号多 2 2)在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数 3)在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数减去前缀右括号个数大于2 (这里这么想,把左变成右,左-1,右+1) 右括号也是这样 代码: #include<bits/stdc++.h> usi

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the follow

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with