暑假集训7月19日

CF补题:
A题:忘记memset,浪费时间
B题:只想到相隔偶数就算,但是没想好用什么方法计算
可以和滚动优化一样,只记录上一次的位置
#include <bits/stdc++.h> using namespace std; const int N=1e5+10; int a[N],pre[N],ans[N]; signed main() { int t; scanf("%d",&t); while (t--) { int n; scanf("%d",&n); memset(pre,0,sizeof pre); memset(ans,0,sizeof ans); for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=1;i<=n;i++) if (!pre[a[i]]||(i-pre[a[i]])%2==1) pre[a[i]]=i,ans[a[i]]++; for (int i=1;i<=n;i++) printf("%d ",ans[i]); printf("\n"); } return 0; }
C题:前缀和
对于偶数,每个配置所需的楼层也可以在O(n)时间里使用交替的前向前缀和数组和交替的后向前缀和数组。

 在多组输入的时候,结束用的是continue

用1表示cool楼,0表示普通楼

n是奇数的时候只需要让所有偶数位置是cool楼;

n是偶数的时候,最多有(n-2)/2个cool楼,也就是说在0和1交替的情况下最多有一个连续的00出现,如上图,首先从下标2开始计算,得到第一种情况下的结果,也就是00在最末尾的情况。然后从后往前遍历,可以发现00位置每次向前移动两位,ans相较于前一种情况是减去cal(i-1)并加上cal(i)的。在枚举00的位置的时候去维护ans的最小值。

cal函数里面0ll的写法表示的是长零,这样可以使得计算过程中不会溢出,结果保证是long long类型

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[N];
ll cal(int i)
{
    return max(0ll,max(a[i-1],a[i+1])-a[i]+1);
}
signed main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n;
        scanf("%d",&n);
        for (int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        if (n&1)
        {
            ll ans=0;
            for (int i=2;i<=n-1;i+=2)
                ans+=cal(i);
            printf("%lld\n",ans);
            continue;
        }
        ll tmp=0;
        for (int i=2; i<=n-1; i+=2)
            tmp+=cal(i);
        ll ans=tmp;
        for (int i=n-1;i>=3;i-=2)
            tmp-=cal(i-1),tmp+=cal(i),ans=min(ans,tmp);
        printf("%lld\n",ans);
    }
    return 0;
}

 

 

 

D - XOR Permutations

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N=1e5+10;

signed main()
{
    int t;
    cin>>t;
    while (t--)
    {
        string s[3];
        for (int i=0; i<3; i++)cin>>s[i];
        int num[3];
        memset(num,0,sizeof num);
        int sum=0;
        for (int i=0; i<3; i++)
            for (int j=0; j<10; j++)
            {
                if (s[i][j]=='1')num[i]++;
            }
        sum=num[0]+num[1]+num[2];
        if (sum<=10)
        {
            for (int i=0; i<sum; i++)
                cout<<1;
            for (int i=sum; i<10; i++)
                cout<<0;
            cout<<"\n";
        }
        else
        {
            sort(num,num+3);
            while (num[0]>0&&sum>10)num[0]--,num[1]--,sum-=2;
            if (sum>10)
            {
                for (int i=0; i<10; i++)
                    if (i<20-sum)cout<<1;比赛的时候脑子糊了,这里没想清楚
                    else cout<<0;
                cout<<"\n";
            }
            else
            {
                for (int i=0; i<10; i++)
                    if (i<sum)cout<<1;
                    else cout<<0;
                cout<<"\n";
            }
        }
    }
    return 0;
}

J - Grid Beauty unordered_map比map快很多

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N=1e3+10;
int a[N][N];
bool st[N][N];
unordered_map<int,int>mp,p;
signed main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++)
                scanf("%d",&a[i][j]);
        ll ans=0;
        for (int i=1; i<=n-1; i++)
        {
            for (int j=1; j<=m; j++)
            {
                mp[a[i][j]]++;
                p[a[i+1][j]]++;
            }
            for (int j=1; j<=m; j++)
                if (mp[a[i][j]]&&p[a[i][j]])
                {
                    mp[a[i][j]]--;
                    p[a[i][j]]--;
                    ans++;
                }
            mp.clear();
            p.clear();
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2022-07-19 08:48  好腻友Π  阅读(39)  评论(0)    收藏  举报