【CodeForces】【map】GYM 101823 A Zero Array

【题目】http://codeforces.com/gym/101853/problem/A

【题意】给出一串数,两种操作,把一个位置的数改变,询问这串数中有多少个不是0的数。

【思路】map存数,另外存答案;map存数,set再存一遍去0;map每次查询数一遍不是0的(TLE)

【代码】

1:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int M=1e5+100;
const int inf=1e9+10;
int a[M];
map<int,int>book;


int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,q;
        int ans=0;
        scanf("%d%d",&n,&q);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            if(book[a[i]]==0&&a[i]!=0)
                ans++;
            book[a[i]]++;
        }
        for(int i=1; i<=q; i++)
        {
            int tt;
            scanf("%d",&tt);
            if(tt==2)
            {
                printf("%d\n",ans);
            }
            if(tt==1)
            {
                int p,v;
                scanf("%d%d",&p,&v);
                book[a[p]]--;
                if(book[a[p]]==0&&a[p]!=0)
                    ans--;
                a[p]=v;
                if(book[a[p]]==0&&a[p]!=0)
                    ans++;
                book[a[p]]++;
            }
        }
        book.clear();
    }
}

 2:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int M=1e5+100;
const int inf=1e9+10;
ll a[M];
map<ll,int>book;


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,q;
        set<ll> s;
        scanf("%d%d",&n,&q);
        for(int i=1; i<=n; i++)
        {
            scanf("%I64d",&a[i]);
            if(a[i] != 0) s.insert(a[i]);
            book[a[i]]++;
        }
        for(int i=0; i<q; i++)
        {
            int tt;
            scanf("%d",&tt);
            if(tt==2)
            {
                printf("%d\n",s.size());
            }
            else
            {
                int p;
                ll v;
                scanf("%d%I64d",&p,&v);
                book[a[p]]--;
                if(book[a[p]] == 0 && a[p] != 0) s.erase(a[p]);
                a[p]=v;
                book[a[p]]++;
                if(v != 0) s.insert(v);
            }
        }
        book.clear();
    }
    return 0;
}

TLE:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int M=1e5+100;
const int inf=1e9+10;
int a[M];
map<int,int>book;


int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            book[a[i]]++;
        }
        for(int i=1; i<=q; i++)
        {
            int tt;
            scanf("%d",&tt);
            if(tt==2)
            {
                int cnt=0;
                map<int, int>::iterator iter;
                for(iter = book.begin(); iter != book.end(); iter++)
                {
                    if(iter->second!=0&&iter->first!=0)
                        cnt++;
                }
                printf("%d\n",cnt);
            }
            if(tt==1)
            {
                int p,v;
                scanf("%d%d",&p,&v);
                book[a[p]]--;
                a[p]=v;
                book[a[p]]++;
            }
        }
        book.clear();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值