Codeforces #340 div2 ABCDE

套题链接:http://codeforces.com/contest/617

难度类型:ABCD较易,其中CD比一般比赛简单很多,尤其是D,E需要使用莫队算法,补题让我成功学到了新姿势。BCD均有一些坑点,hack点较多,赛中成功黑了十多次。

这场打得不错,但还有不少可以改进的地方,一个是D交得有些太过草率,另外B可以锁了去黑,还有就是最后应该抓紧黑。

A

题解

类型:数学

x/5 就是答案。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head

int main()
{
    int x;
    scanf("%d", &x);
    printf("%d\n", (x+4)/5);
    return 0;
}

B

题解

类型:数学

首先一个黑点是全部为 0 的情况,这个特判即可。当至少有一个1两边的 0 应该就可以固定不考虑了。找到左右起始的1找到每段 0 leni,答案是 leni

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 100+5;

int a[N];
int main()
{
    int n, l = 105, r = -1;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", a+i);
        if (a[i]) {
            l = min(l, i);
            r = max(r, i);
        }
    }

    if (l == 105) {
        puts("0");
    } else {
        LL ans = 1;
        int len = 0;
        for (int i = l; i <= r; i++) {
            if (a[i]) {
                ans *= len+1;
                len = 0;
            } else {
                len++;
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

C

题解

类型:枚举,几何

首先很容易发现,喷泉覆盖的边缘必然是花,于是可以平方去枚举。需要注意的是喷泉可能不覆盖任何花,用这个点黑了很多人。然后还有结果是LL的。这题也有 Θ(nlogn) 的做法,有兴趣的可以去cf看。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 2005;

LL dissq(LL x1, LL y1, LL x2, LL y2) {
    return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
}

LL x1, x2, y1, y2;
int n;

LL x[N];
LL y[N];

LL solve(LL cx, LL cy) {
    LL dis1 = dissq(x1, y1, cx, cy);
    LL dis2 = 0;
    for (int i = 0; i < n; i++) {
        if (dissq(x1, y1, x[i], y[i]) > dis1) {
            dis2 = max(dis2, dissq(x2, y2, x[i], y[i]));
        }
    }
    return dis1+dis2;
}
int main()
{
    scanf("%d%I64d%I64d%I64d%I64d", &n, &x1, &y1, &x2, &y2);
    for (int i = 0; i < n; i++) {
        scanf("%I64d%I64d", x+i, y+i);
    }

    LL ans = 1e18;
    ans = min(ans, solve(x1, y1));
    for (int i = 0; i < n; i++) {
        ans = min(ans, solve(x[i], y[i]));
    }

    printf("%I64d\n", ans);
    return 0;
}

D

题解

类型:想法,几何

很容易发现结果在 [1,3] 之间, 1 的情况就是属于一条纵轴或者横轴。2的情况需要注意,两点 x 相同,第三个y不在前两个点的中间或者两点 y 相同,第三个的x不在前两个点的中间,可以利用排列来枚举。余下的输出 3 即可。以下为2的情况,赛中我漏考虑了导致被黑。

这里写图片描述

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head

PII a[3];

bool bet(int a, int x, int y) {
    return a > min(x, y) && a < max(x, y);
}

int judge() {
    if (a[0].fi == a[1].fi && a[0].fi == a[2].fi) return 1;
    if (a[0].se == a[1].se && a[0].se == a[2].se) return 1;

    int b[3] = {0, 1, 2};
    do {
        if (a[b[0]].fi == a[b[1]].fi && !bet(a[b[2]].se, a[b[0]].se, a[b[1]].se)) return 2;
        if (a[b[0]].se == a[b[1]].se && !bet(a[b[2]].fi, a[b[0]].fi, a[b[1]].fi)) return 2;
    } while (next_permutation(b, b+3));

    return 3;
}
int main()
{
    for (int i = 0; i < 3; i++) {
        scanf("%d%d", &a[i].fi, &a[i].se);
    }
    printf("%d\n", judge());
    return 0;
}

E

题解

类型:莫队算法

利用离线的莫队算法解决,整个算法还是比较容易的基本上学一会就能了解,是改造算法以降低复杂度的方法,复杂度从平方降为 Θ(nn)

我学习的参考资料:
https://www.hackerearth.com/notes/mos-algorithm/
http://blog.anudeep2011.com/mos-algorithm/
http://blog.csdn.net/bossup/article/details/39236275

本题要求异或的个数,可以维护异或前缀和赖在常数时间内对左右区间的边界进行移动。需要注意左边界要-1,因为除了前缀的异或前缀本身也是结果,这个我想了一晚上才想通。。。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1E5+5;
const int M = 1<<20;

int pos[N];
int a[N];
int cnt[M];

struct Node {
    int l, r, id;
};

bool cmp(const Node &a, const Node &b) {
    return (pos[a.l] == pos[b.l]) ? a.r < b.r : pos[a.l] < pos[b.l];
}

Node query[N];
LL res[N];

int n, m, k;
LL ans = 0;
void add(int x) {
    ans += cnt[x^k];
    cnt[x]++;
}
void rem(int x) {
    cnt[x]--;
    ans -= cnt[x^k];
}
int main()
{
  scanf("%d%d%d", &n, &m, &k);
  int blk = sqrt(n);
  for (int i = 1; i <= n; i++) {
        scanf("%d", a+i);
        a[i] ^= a[i-1];
        pos[i] = i/blk;
  }

  for (int i = 0; i < m; i++) {
        scanf("%d%d", &query[i].l, &query[i].r);
        query[i].id = i;
  }
  sort(query, query+m, cmp);

  int l = 1, r = 0;
  for (int i = 0; i < m; i++) {
        while (r < query[i].r) add(a[++r]);
    while (r > query[i].r) rem(a[r--]);
    while (l < query[i].l-1) rem(a[l++]);
    while (l > query[i].l-1) add(a[--l]);
    res[query[i].id] = ans;
  }

  for (int i = 0; i < m; i++) {
        printf("%I64d\n", res[i]);
  }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值