码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #484 (Div. 2)

时间:2018-05-20 15:26:45      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:its   targe   pop   quota   队列   top   empty   人做   XA   

                                A. Row
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

You‘re given a row with nn chairs. We call a seating of people "maximal" if the two following conditions hold:

  1. There are no neighbors adjacent to anyone seated.
  2. It‘s impossible to seat one more person without violating the first rule.

The seating is given as a string consisting of zeros and ones (00 means that the corresponding seat is empty, 11 — occupied). The goal is to determine whether this seating is "maximal".

Note that the first and last seats are not adjacent (if n2n≠2).

Input

The first line contains a single integer nn (1n10001≤n≤1000) — the number of chairs.

The next line contains a string of nn characters, each of them is either zero or one, describing the seating.

Output

Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No".

You are allowed to print letters in whatever case you‘d like (uppercase or lowercase).

Examples
Input
Copy
3
101
Output
Copy
Yes
Input
Copy
4
1011
Output
Copy
No
Input
Copy
5
10001
Output
Copy
No
Note

In sample case one the given seating is maximal.

In sample case two the person at chair three has a neighbour to the right.

In sample case three it is possible to seat yet another person into chair three.

题意:给出一个字符串,0表示空位,1表示有人占位,人和人不能坐一起,且给出的序列没有其他人可以坐下了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mem(a,b) memset(a,b,sizeof(a))
#define P pair< int , int >
const int maxn = 100000+10;
const int INF = 0x3f3f3f3f3f;
char a[1005];
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",a+1);
    if(n==1)
    {
        if(a[1]==0)
            printf("No\n");
        else printf("Yes\n");
        return 0;
    }
    if((a[1]==0&&a[2]==0)||(a[n-1]==0&&a[n]==0))
    {
        printf("No\n");
        return 0;
    }
    int cnt1=0,cnt2=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==0)
            cnt1++;
        else cnt1=0;
        if(a[i]==1)
            cnt2++;
        else cnt2=0;
        if(cnt1>=3||cnt2>=2)
        {
            printf("No\n");
            return 0;
        }
    }
    printf("Yes\n");

}
                                B. Bus of Characters
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the Bus of Characters there are nn rows of seat, each having 22 seats. The width of both seats in the ii-th row is wiwi centimeters. All integers wiwi are distinct.

Initially the bus is empty. On each of 2n2n stops one passenger enters the bus. There are two types of passengers:

  • an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it;
  • an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it.

You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take.

Input

The first line contains a single integer nn (1n2000001≤n≤200000) — the number of rows in the bus.

The second line contains the sequence of integers w1,w2,,wnw1,w2,…,wn (1wi1091≤wi≤109), where wiwi is the width of each of the seats in the ii-th row. It is guaranteed that all wiwi are distinct.

The third line contains a string of length 2n2n, consisting of digits ‘0‘ and ‘1‘ — the description of the order the passengers enter the bus. If the jj-th character is ‘0‘, then the passenger that enters the bus on the jj-th stop is an introvert. If the jj-th character is ‘1‘, the the passenger that enters the bus on the jj-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal nn), and for each extrovert there always is a suitable row.

Output

Print 2n2n integers — the rows the passengers will take. The order of passengers should be the same as in input.

Examples
input
Copy
2
3 1
0011
output
Copy
2 1 1 2 
input
Copy
6
10 8 9 11 13 5
010010011101
output
Copy
6 6 2 3 3 1 4 4 1 2 5 5 
Note

In the first example the first passenger (introvert) chooses the row 22, because it has the seats with smallest width. The second passenger (introvert) chooses the row 11, because it is the only empty row now. The third passenger (extrovert) chooses the row 11, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 22, because it is the only row with an empty place.

 题意:给出n个座位的宽度(每个座位可以坐两个人),给出一个01串,0表示内向的人(喜欢一个人,且喜欢宽度小的座位),1表示外向的人(喜欢别人做,且喜欢宽度大的座位),0和1的个数相等 且内向的人,都能选到自己喜欢的座位

这里我们给输入的宽度排序,然后每次遇到0,我们就把入优先队列(就是座位的另一半)(优先队列降序排),然后每次1 就取队首就行了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mem(a,b) memset(a,b,sizeof(a))
#define P pair< int , int >
const int maxn = 200000+10;
const int INF = 0x3f3f3f3f3f;
struct node
{
    int w;
    int id;
    bool friend operator < (const node &u,const node &v)
    {
        return u.w<v.w;
    }
} p[maxn];
bool cmp(const node &u,const node &v)
{
    return u.w<v.w;
}
int main()
{
    priority_queue<node> q;
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&p[i].w);
        p[i].id=i;
    }
    sort(p+1,p+1+n,cmp);
    char s[maxn*2];
    int t=1;
    scanf("%s",s+1);
    for(int i=1; i<=2*n; i++)
    {
        if(s[i]==0)
        {
            printf("%d ",p[t].id);
            q.push(p[t++]);
        }
        else
        {
            printf("%d ",q.top().id);
            q.pop();
        }
    }
}
                          C. Cut ‘em all!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You‘re given a tree with nn vertices.

Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.

Input

The first line contains an integer nn (1n1051≤n≤105) denoting the size of the tree.

The next n?1n?1 lines contain two integers uu, vv (1u,vn1≤u,v≤n) each, describing the vertices connected by the ii-th edge.

It‘s guaranteed that the given edges form a tree.

Output

Output a single integer kk — the maximum number of edges that can be removed to leave all connected components with even size, or ?1?1if it is impossible to remove edges in order to satisfy this property.

Examples
input
Copy
4
2 4
4 1
3 1
output
Copy
1
input
Copy
3
1 2
1 3
output
Copy
-1
input
Copy
10
7 1
8 4
8 10
4 7
6 5
9 3
3 5
2 10
2 5
output
Copy
4
input
Copy
2
1 2
output
Copy
0
Note

In the first example you can remove the edge between vertices 11 and 44. The graph after that will have two connected components with two vertices in each.

In the second example you can‘t remove edges in such a way that all components have even number of vertices, so the answer is ?1?1.

如果点数是计数,那么直接输出-1。否则,我们可以从任意位置开始dfs,遍历整棵树,遍历的时候。当子树的节点树为偶数的时候,我们可以将其分割,因为偶数段,分割任然为偶数。如果是奇数,那么我们把它加上它的子树节点,然后继续遍历。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mem(a,b) memset(a,b,sizeof(a))
#define P pair< int , int >
const int maxn = 100000+10;
const int INF = 0x3f3f3f3f3f;
int head[maxn],cnt=0,son[maxn];
int ans=0;
struct node
{
    int to,next;
} p[maxn*200];
void add(int u,int v)
{
    p[++cnt].next=head[u];
    head[u]=cnt;
    p[cnt].to=v;
}
void dfs(int u,int f)
{
    son[u]=1;
    for(int i=head[u]; i; i=p[i].next)
    {
        int v=p[i].to;
        if(v!=f)
        {
            dfs(v,u);
            if(son[v]>1&&son[v]%2==0)
                ans++;
            else son[u]+=son[v];
        }
    }
}
int main()
{
    int n,u,v;
    scanf("%d",&n);
    for(int i=1; i<n; i++)
    {
        scanf("%d %d",&u,&v);
        add(u,v);
        add(v,u);
    }
    if(n&1)
    {
        printf("-1\n");
        return 0;
    }
    dfs(1,0);
    printf("%d\n",ans);
}

 

 

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

Codeforces Round #484 (Div. 2)

标签:its   targe   pop   quota   队列   top   empty   人做   XA   

原文地址:https://www.cnblogs.com/MengX/p/9063367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!