Codeforces数据结构(水题)小结

最近在使用codeblock,所以就先刷一些水题上上手

使用codeblock遇到的问题

1.无法进行编译-------从setting中的编译器设置中配置编译器

2.建立cpp后无法调试------只建立源文件无法调试,需要建立一个工程后才能调试

3.设置断点后,调试不会停止------开启-g模式且工程要建立在一个没有中文名的文件夹下

4.调试中如何查看变量------打开debug中的watch,右键编辑界面的变量可以选择添加变量

 

水题来源---codeforces(hzwer神犇刷的水题)

570C.Replacement

给定一个长为n的字符串(包含小写字母和’.’),有m次操作
每次操作可以修改字符,并询问修改后有多少对相邻的’.’
1n,m105

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
    int n, q, N = 0;
    int x; char ch;
    string str;
    cin>>n>>q>>str;
    int l = str.length();
    for(int i = l; i >= 1; i--) str[i] = str[i-1];
    str[0] = '#'; str[l+1] = '#';
    for(int i = 1; i <= l; i++)
        if(str[i] == '.' && str[i-1] == '.') N++;
    while(q--)
    {
        cin>>x>>ch;
        if(ch != '.')
        {
            if(str[x] == '.')
            {
                if(str[x-1] == '.') N--;
                if(str[x+1] == '.') N--;
            }
        } else
        {
            if(str[x] != '.')
            {
                if(str[x-1] == '.') N++;
                if(str[x+1] == '.') N++;
            }
        }
        str[x] = ch;
        cout<<N<<endl;
    }
}

 

427B.Prison Transfer

给定长为 n 的序列,以及 c,t
问你有多少个连续的长度为 c 的子串,且序列中没有一个超过 t

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int n, t, c, N = 0, ans = 0, x;
    cin>>n>>t>>c;
    for(int i = 1; i <= n; i++)
    {
        cin>>x;
        if(x <= t) N++;
        if(x > t) N = 0;
        if(N >= c) ans++;
    }
    cout<<ans<<endl;
}

 

519B.A and B and Compilation Errors

给定三个序列,长度分别为 n,n-1,n-2,每一行是上一行的序列去掉一个元素

要求输出去掉的元素

题解:直接排序对比即可

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100500;
int n;
int a[maxn], b[maxn], c[maxn];
int main()
{
    cin>>n;
    for(int i = 1; i <= n; i++) cin>>a[i];
    for(int i = 1; i <= n-1; i++) cin>>b[i];
    for(int i = 1; i <= n-2; i++) cin>>c[i];
    sort(a+1, a+1+n);
    sort(b+1, b+n);
    sort(c+1, c+n-1);
    for(int i = 1; i <= n; i++) if(a[i] != b[i]) { cout<<a[i]<<endl; break; }
    for(int i = 1; i <= n-1; i++) if(b[i] != c[i]) { cout<<b[i]<<endl; break; }

}

 

650A.Watchmen

给定二维平面的n个坐标,求满足曼哈顿距离等于几何距离的点对

题解:按照x排序,再按照y排序,统计出来即可

*注意需要去掉重复点自身构成的点对(简单容斥)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
struct Point
{
    int x, y;
}p[200050];

bool cmp1(const Point &A, const Point &B)
{ return A.x > B.x; }
bool cmp2(const Point &A, const Point &B)
{ return (A.y == B.y) ? A.x > B.x : A.y > B.y; }
int n;
LL ans = 0;
int main()
{
    cin>>n;
    for(int i = 1; i <= n; i++) cin>>p[i].x>>p[i].y;
    sort(p+1, p+1+n, cmp1);
    LL t = 1, t2 = 1;
    for(int i = 2; i <=n; i++)
    {
        if(p[i].x == p[i-1].x) t++;
        else
        {
            ans += (t*(t-1)/2);
            t = 1;
        }
    }
    ans += (t*(t-1)/2); t = 1;
    sort(p+1, p+1+n, cmp2);
    for(int i = 2; i <= n; i++)
    {
        if(p[i].x == p[i-1].x && p[i].y == p[i-1].y) t2++;
        else
        {
            ans -= (t2*(t2-1)/2);
            t2 = 1;
        }
        if(p[i].y == p[i-1].y) t++;
        else
        {
            ans += (t*(t-1)/2);
            t = 1;
        }
    }
    ans += (t*(t-1)/2);
    ans -= (t2*(t2-1)/2);
    cout<<ans<<endl;
}

 

466C.Number of Ways

给定长为n的序列aiai,将其分成3个连续子串,要求每个子串的和相同,求划分的方案数

题解:统计所有满足sum[i]*3 = sum[n]的点的数量m;然后倒序枚举,每枚举到sum[i]*3/2 = sum[n]的点,就加上m,每枚举到sum[i]*3 = sum[n]的点,m--,最后输出即可

*注意当m<=0时,及时跳出,不然会使答案减少

#include <iostream>
#include <cstdio>
using namespace std;
int sum[500050], a[500050];
int main()
{
    int n;
    long long f = 0, ans = 0;
    cin>>n;
    for(int i = 1; i <= n; i++) cin>>a[i];
    sum[1] = a[1];
    for(int i = 2; i <= n; i++) sum[i] = a[i] + sum[i-1];
    for(int i = 1; i < n-1; i++) if(sum[i]*3 == sum[n]) f++;
    for(int i = n-1; i > 1; i--)
    {
        if(sum[i]*3 == sum[n]*2) ans += f;
        if(sum[i]*3 == sum[n]) f--;
        if(f <= 0) break;  //f会出现负值的情况
    }
    cout<<ans<<endl;
}

 

转载于:https://www.cnblogs.com/Saurus/p/5998598.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值