首页 > 代码库 > Codeforces Round #394 (Div. 2)

Codeforces Round #394 (Div. 2)

传送门:http://codeforces.com/contests/763,764

A题:[l,r]中有a个偶数,b个奇数,问存不存在l和r,1<=l<=r。很容易想到,如果abs(a-b)<=1,那么l和r就是存在的,除了一种情况,就是a=b=0的时候。由于l和r都大于等于1,所以当a=b=0时,不存在这种情况

技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define in() freopen("data","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;

int main()
{
    int a, b;
    cin >> a >> b;
    if (a == 0 || b == 0) puts("NO");
    else if (b == a || b == a + 1 || a == b + 1) puts("YES");
    else puts("NO");
    return 0;
}
View Code

B题:有n个障碍,还有L长度的圈。分别给出A和B遇到这n个障碍的时间,问他们是否在同一个轨道上。直接处理出时间差,然后对时间差不断旋转,看是否匹配。

技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define in() freopen("data","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int N[2][210];
vector <int> temp, temp2;
int main()
{
    int n, L;
    cin >> n >> L;
    for (int i = 0; i < n; i++) cin >> N[0][i];
    for (int i = 0; i < n; i++) cin >> N[1][i];
    int num = L;
    for (int i = 1; i < n; i++)
    {
        temp.pb(N[0][i] - N[0][i-1]);
        num -= N[0][i] - N[0][i-1];
    }
    temp.pb(num);
    num = L;
    for (int i = 1; i < n; i++)
    {
        temp2.pb(N[1][i] - N[1][i-1]);
        num -= N[1][i] - N[1][i-1];
    }
    temp2.pb(num);
    int sz = temp.size();
    for (int j = 0; j < sz; j++)
    {
        int flag = 1;
        for (int i = 0; i < sz; i++)
        {
            if (temp2[i] != temp[(j+i)%sz])
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            puts("YES");
            return 0;
        }
    }
    puts("NO");
    return 0;
}
View Code

C题:给你n个字符串,每个字符串长度都为m,然后有n个光标,每个光标在每个字符串的开头,光标可以左右移动,问光标最少移动多少次,才能光标位置上的字符组合成的字符串包含至少一个符号,一个字母,一个数字。只要把每个字符串的光标移动后得到符号、字母、数字的最小次数预处理出来,然后记录下id,再排序,然后3个for暴力找最优解。只需要找每项的前三个就好了。这种是贪心的方法,也可以不排序,直接暴力3个for查询全部,不过复杂度是n3,也可以过。

技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define in() freopen("data","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int INF = 10100;
char str[110][110];
pii F[110], N[110], C[110];

int main()
{
    int n, len;
    cin >> n >> len;
    for (int i = 0; i < n; i++)
        cin >> str[i];
    for (int i = 0; i < n; i++)
    {
        F[i].X = N[i].X = C[i].X = INF;
        F[i].Y = N[i].Y = C[i].Y = i;
        for (int j = 0; j < len; j++)
        {
            int pos = min(j, len - j);
            if (str[i][j] >= 0 && str[i][j] <= 9)
                N[i].X = min(N[i].X, pos);
            else if (str[i][j] >= a && str[i][j] <= z)
                C[i].X = min(C[i].X, pos);
            else F[i].X = min(F[i].X, pos);
        }
    }
    sort(F, F + n);
    sort(N, N + n);
    sort(C, C + n);
    int ans = INF;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            for (int k = 0; k < 3; k++)
            {
                if (N[i].Y != F[j].Y && F[j].Y != C[k].Y && N[i].Y != C[k].Y)
                    ans = min(ans, N[i].X + F[j].X + C[k].X);
            }
    cout << ans << endl;
    return 0;
}
View Code

D题:给你n个数,和一个范围l和r,接下来有数组a[n],下一行是数组p[n],假设有数组c[n],他按数字大小的排名对应p[n],也可以看做p[n]是c[n]的离散化。问存不存在数组b,使得c[i]=b[i]-a[i],且b[i]的范围在[l,r]中。可以先对p排序,然后从小到大处理出c的值,我们假设p={1,2,3,4,5},那么c[0]一定等于l,c[i]=max(l,c[i-1]-a[i-1]+a[i]+1);下标要按离散化的下标进行操作。

技术分享
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define in() freopen("data","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[maxn];
pii p[maxn];
int ans[maxn];
int main()
{
    // ios::sync_with_stdio(false);
    int n, l, r;
    scanf("%d%d%d", &n, &l, &r);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &p[i].X);
        p[i].Y = i;
    }
    sort(p, p + n);
    int pos = l;
    ans[p[0].Y] = l;
    for (int i = 1; i < n; i++)
    {
        pos =  max(l, pos + a[p[i].Y] - a[p[i-1].Y] + 1);
        ans[p[i].Y] = pos;
        if (pos > r)
        {
            puts("-1");
            return 0;
        }
    }
    for (int i = 0; i < n; i++) printf("%d ", ans[i]);
    puts("");
    return 0;
}
View Code

E题:给你一棵节点为n的树,让你在平面直角坐标系上画出这样一棵树,他的边必须平行或垂直于x轴,边长不限,点坐标绝对值不超过1e18。问能否构造出这棵树,且边不相交,如果能,输出点的坐标。这题要构造出一组边长序列,用N[i]代表边长,sum[i]代表N[i]~N[n-1]的和(n个点的树有n-1条边),那么N[i]必须要大于sum[i+1]。可以考虑,当N为2的幂的时候,满足N的要求。(2>1,4>1+2,8>1+2+4),于是边长序列就构造出来了。我们从边长为1<<n开始,一路dfs下去,上下左右递归,每个点最多有3个子节点和一个父节点(超过输出NO),递归后边长/2,这样就保证了边长不会相交,至于为什么,可以自己画一下图。

技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define in() freopen("data","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e3 + 10;
const int INF = 0x3f3f3f3f;
int head[maxn], cnt , vis[maxn], flag;
pii ans[maxn];
const int Next[4][2] = {{1, 0}, {0, 1}, {0, -1}, { -1, 0}};
struct Edge
{
    int v, to;
} E[maxn];

void init()
{
    clr(head, -1);
    cnt = 0;
}
void addedge(int u, int v)
{
    E[cnt].v = u, E[cnt].to = head[v];
    head[v] = cnt++;
}

void dfs(int cur, int x, int y, int L, int k)
{
    if (vis[cur]) return ;
    vis[cur] = 1;
    ans[cur] = make_pair(x, y);
    int pos = 0, sum = 0;
    for (int i = head[cur]; ~i; i = E[i].to)
    {
        sum++;
        int v = E[i].v;
        if (vis[v]) continue;
        if (pos + k == 3) pos++;
        dfs(v, x + Next[pos][0]*L, y + Next[pos][1]*L, L >> 1, pos);
        pos++;
    }
    if (sum > 4) flag = 1;
}

int main()
{
    init();
    int n;
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        addedge(u, v), addedge(v, u);
    }
    dfs(1, 0, 0, 1 << 30, 4);
    if (flag) cout << "NO" << endl;
    else
    {
        cout << "YES" << endl;
        for (int i = 1; i <= n; i++)
            cout << ans[i].X << " " << ans[i].Y << endl;
    }
    return 0;
}
View Code

2017-02-02 21:11:39

Codeforces Round #394 (Div. 2)