AtCoder Beginner Contest 174 总结

这次做了ABDF。我也不知道我怎么做的(乱做-.-

A - Air Conditioner

签到题1

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define debug(x) cout<<#x<<": "<<x<<" "
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    IO;
    int T;
    cin>>T;
    if(T>=30) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

B - Distance

签到题2

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define debug(x) cout<<#x<<": "<<x<<" "
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
    IO;
    int n;
    ll d;
    cin>>n>>d;
    int res=0;
    while(n--)
    {
        ll x,y;
        cin>>x>>y;
        if(x*x+y*y<=d*d) res++;
    }
    cout<<res<<endl;
    return 0;
}

C - Repsept

群里大佬说k是2或5的倍速显然不行,其他再int范围内显然有解(为啥我看不出来显然)。反正实在不行就暴力长度 1 1 0 7 1\dots10^7

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
    IO;
    ll k;
    cin>>k;
    if(k%2==0||k%5==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ll sum=0,len=1,p=1;
    while(true)
    {
        sum=(sum+7*p)%k;
        if(sum==0) 
        {
            cout<<len<<endl;
            break;
        }
        len++;
        p=p*10%k;
    }
    return 0;
}

反思:好几次看到这种大数的都想着高精度,而且每次看到题解的时候都是因为模了一个数最终避免高精度。下次想高精度的时候需要考虑下是否能过模一个数避免高精度

D - Alter Altar

最终R全在左边,W全在右边,按照快排时候的策略,贪心+双指针。

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define debug(x) cout<<#x<<": "<<x<<" "
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
int main()
{
    IO;
    int n;
    string s;
    cin>>n>>s;
    int i=0,j=n-1;
    int res=0;
    while(i<j)
    {
        if(s[i]=='R'&&s[j]=='W') i++,j--;
        else if(s[i]=='W'&&s[j]=='R') res++,i++,j--;
        else if(s[i]=='W'&&s[j]=='W') j--;
        else i++;
    }
    cout<<res<<endl;
    return 0;
}

E - Logs

最大值最小二分答案。u1s1这个题不难。考试怎么没做!!!wctl

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=200010;
int a[N],n,k;
bool check(int mid)
{
    ll res=0;
    for(int i=1;i<=n;i++) res+=(a[i]+mid-1)/mid-1;
    return res<=k;
}
int main()
{
    IO;
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    int l=1,r=1e9;
    while(l<r)
    {
        int mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    cout<<l<<endl;
    return 0;
}

F - Range Set Query

解法一:莫队板子题 5 × 1 0 5 5×10^5 过不了?那我开个O2还真让我水过了(滑稽
O ( m n ) O(m\sqrt{n}) 1953 ms

#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=500010;
int pos[N],sz;
int cnt[N];
int a[N];
int ans[N];
ll res;
struct node
{
    int l,r,k;
    bool operator <(const node& o)const
    {
        if(pos[l]==pos[o.l]) return r<o.r;
        return pos[l]<pos[o.l];
    }
}q[N];
int n,m;
void add(int k)
{
    cnt[a[k]]++;
    if(cnt[a[k]]==1) res++;
}
void sub(int k)
{
    cnt[a[k]]--;
    if(cnt[a[k]]==0) res--;
}
int main()
{
    scanf("%d%d",&n,&m);
    sz=sqrt(n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        pos[i]=i/sz;
    }
    for(int i=0;i<m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        q[i]={l,r,i};
    }
    sort(q,q+m);
    int l=1,r=0;
    for(int i=0;i<m;i++)
    {
        while(l<q[i].l) sub(l++);
        while(l>q[i].l) add(--l);
        while(r<q[i].r) add(++r);
        while(r>q[i].r) sub(r--);
        ans[q[i].k]=res;
    }
    for(int i=0;i<m;i++) printf("%lld\n",ans[i]);
    return 0;
}

解法二:离线树状数组
对于求不同数的个数,给定区间一个数出现n次对答案的贡献只是1,我们不妨让每次贡献都看作是该区间最后这个数的出现给予的(前面出现的不算答案的贡献)。树状数组维护每个数对答案的贡献。
O ( m l o g n + n ) O(mlogn+n) 222 ms

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=500010;
int tree[N];
int a[N],last[N];//a[]原数组 last[x]动态记录x再原数组最后一次出现的位置
int ans[N];
int n,m;
struct node
{
    int l,r,k;
    bool operator <(const node& o)const
    {
        return r<o.r;
    }
}q[N];
int lowbit(int x)
{
    return x&-x;
}
void add(int x,int c)
{
    for(;x<=n;x+=lowbit(x)) tree[x]+=c;
}
int sum(int x)
{
    int res=0;
    for(;x;x-=lowbit(x)) res+=tree[x];
    return res;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=0;i<m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        q[i]={l,r,i};
    }
    sort(q,q+m);//按照区间右端点排序
    int k=1;//指针 动态维护到的位置(每次维护到询问右端点)
    for(int i=0;i<m;i++)//每一个询问
    {
        for(;k<=q[i].r;k++)
        {
            int x=a[k];
            if(last[x]) add(last[x],-1);//说明x之前出现过 在之前出现的位置减1
            add(k,1);//该位置产生了一个新种类的数
            last[x]=k;
        }
        ans[q[i].k]=sum(q[i].r)-sum(q[i].l-1);
    }
    for(int i=0;i<m;i++) printf("%lld\n",ans[i]);
    return 0;
}

要加油哦~

猜你喜欢

转载自blog.csdn.net/Fighting_Peter/article/details/107779272
今日推荐