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;
}

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;
}

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;
}

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;
}

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;
}

2017-02-02 21:11:39

时间: 2024-08-14 02:29:13

Codeforces Round #394 (Div. 2)的相关文章

Codeforces Round #394 (Div. 2) 解题报告

开始补题,今天下午virtual参赛,过了ABC,D题因为一点小错误而没能在比赛时间中AC,时间到了之后几分钟就发现了问题所在,略有遗憾.之后一直冥思苦想E题,在提示下终于明白,真的是给这样组合题画风的题目跪了,只能说继续加油,扩展思路吧. A题 题目地址 只有奇偶数个数相差小于等于1时可以,需要特判不能使二者均为0的情况. 参考代码 1 #include<stdio.h> 2 #include<bits/stdc++.h> 3 #include <iostream>

【枚举】Codeforces Round #394 (Div. 2) C. Dasha and Password

纪念死去的智商(虽然本来就没有吧--) 三重循环枚举将哪三个fix string作为数字.字母和符号位.记下最小的值就行了. 预处理之后这个做法应该是O(n^3)的,当然完全足够.不预处理是O(n^3*m)的,也够. 我写了一个O(n^2+n*m)的分类讨论贪心做法--蜜汁错,容我查一下. 现在查出个错,交了一下在in queue--容我明天看看对不对. 如果对的话,这题的加强版明年留作趣味赛题吧,嘿嘿嘿-- #include<cstdio> #include<algorithm>

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un