Codeforces Global Round 20(C D)

C. Unequal Array

思路

l为最小的f[l]=f[l+f[1]
r为最大的f[r]=f[r+1]
将这两个合并为一个时,中间的肯定已经没了

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define fir(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
#define met(x,y) memset(x,y,sizeof x)
//#define read(x) scanf("%d",&x)
using namespace std;
const int N=2e5+10;
int f[N];
int read()
{
char ch = getchar(); int x = 0, w = 1;
while (ch < '0' || ch>'9') { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0';
ch = getchar(); }
return x*w;
}
int main()
{
//freopen("test.txt", "r", stdin);
int t=read();
while(t--)
{
int n=read();
int l=0,r=0;
for(int i=1;i<=n;i++)f[i]=read();
for(int i=1;i<n;i++)
{
    if(f[i]==f[i+1])
    {
        if(l==0)l=i;
        r=i;
    }
}
if(l==r)puts("0");
else {
    int mid=max(r-l-1,1);
    cout<<mid<<endl;
}
}
//freopen("CON", "r", stdin);
//system("pause");
 return 0;
}

D. Cyclic Rotation

思路

a和b都从开头进行遍历
用一个cnt[f[i]]有几个f[i]移到后面去了
当f[i]==f[j]此时的f[i]有可能是从前面替换到后面
if cnt[f[i]]>0 cnt[f[i]]–,j++(注意i不变)
else i++,j++
当 f[i]!=f[j] 说明 f[i]要向后移动 cnt[f[i]]++,i++;

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include<map>
#define fir(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
#define met(x,y) memset(x,y,sizeof x)
//#define read(x) scanf("%d",&x)
using namespace std;
const int N=2e5+10;
int a[N],b[N];
map<int,int> p;
int read()
{
char ch = getchar(); int x = 0, w = 1;
while (ch < '0' || ch>'9') { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0';
ch = getchar(); }
return x*w;
}
int main()
{
//freopen("test.txt", "r", stdin);
int t=read();
while(t--)
{
    p.clear();
    int n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=n;i++)b[i]=read();
    int i,j;
    for(i=1,j=1;j<=n&&i<=n;)
    {
        if(a[i]==b[j]&&p[a[i]])p[a[i]]--,j++;
        else if(a[i]==b[j]&&p[a[i]]==0)i++,j++;
        else p[a[i]]++,i++;
    }
    if(i==n+1&&j==n+1)puts("YES");
    else puts("NO");
}
//freopen("CON", "r", stdin);
//system("pause");
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值