Codeforces Round #303 (Div. 2)

A. Toy Cars

Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.

There are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n × n matrix А: there is a number on the intersection of the і-th row and j-th column that describes the result of the collision of the і-th and the j-th car:

 - 1: if this pair of cars never collided.  - 1 occurs only on the main diagonal of the matrix.
0: if no car turned over during the collision.
1: if only the i-th car turned over during the collision.
2: if only the j-th car turned over during the collision.
3: if both cars turned over during the collision.
Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?

Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of cars.

Each of the next n lines contains n space-separated integers that determine matrix A.

It is guaranteed that on the main diagonal there are  - 1, and  - 1 doesn’t appear anywhere else in the matrix.

It is guaranteed that the input is correct, that is, if Aij = 1, then Aji = 2, if Aij = 3, then Aji = 3, and if Aij = 0, then Aji = 0.

Output
Print the number of good cars and in the next line print their space-separated indices in the increasing order.

Sample test(s)
input
3
-1 0 0
0 -1 1
0 2 -1
output
2
1 3
input
4
-1 3 3 3
3 -1 3 3
3 3 -1 3
3 3 3 -1
output
0

  • 这题就是在矩阵中,第i行的第j列表示第i辆车与第j辆车碰撞,数值代表碰撞结果,最后输出幸存的车的编号,模拟。
#include <stdio.h>
#include <string.h>

int main()
{
    int st[110];
    memset(st, 0, sizeof(st));
    int n, a;
    while(~scanf("%d",&n))
    {
        for(int i = 1; i <= n; i ++)
        {
            for(int j = 1; j <= n; j ++)
            {
                scanf("%d",&a);
                if(!st[i])
                {
                    if(a == 1)
                    {
                        st[i] = 1;
                    }
                    else if(a == 2)
                    {
                        if(!st[j])
                        {
                            st[j] = 1;
                        }
                    }
                    else if(a == 3)
                    {
                        if(!st[j])
                        {
                            st[j] = 1;
                        }
                        st[i] = 1;
                    }
                }
            }   
        }
        int ans = 0;
        for(int i = 1; i <= n; i ++)
        {
            if(!st[i])
               ans ++;  
        }
        printf("%d\n",ans);
        for(int i = 1; i <= n; i ++)
        {
            if(!st[i])
            printf("%d ",i);
        }
        printf("\n");
    }

} 

B. Equidistant String

Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:

We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn’t equal to ti.

As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.

It’s time for Susie to go to bed, help her find such string p or state that it is impossible.

Input
The first line contains string s of length n.

The second line contains string t of length n.

The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.

Output
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line “impossible” (without the quotes).

If there are multiple possible answers, print any of them.

Sample test(s)
input
0001
1011
output
0011
input
000
111
output
impossible
Note
In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.

  • 获得一个和给出字符串一样长度的,与str1,str2的相同长度一致,答案有很多种,输出一种就好,模拟。
#include<stdio.h>
#include<string.h>

char str1[100010];
char str2[100010];
bool s[100010];

int main()
{
    while(~scanf("%s",str1))
    {
        memset(s,false,sizeof(s));

        scanf("%s",str2);
        int len = strlen(str1);
        int cnt = 0; 
        for(int i = 0; i < len; i ++)
        {
            if(str1[i] != str2[i])
            {
                cnt ++; 
            }   
            else
            {
                s[i] = true;
            }
        }
        if(cnt%2 == 1)
        {
            printf("impossible\n");
            continue; 
        }
        int amt = 0;
        for(int i = 0; i < len; i ++)
        {
            if(s[i])
            printf("%c",str1[i]);
            else if(!s[i] && amt < cnt/2)
            {
                amt ++;
                printf("%c",str1[i]);
            }
            else
            {
                printf("%c",str2[i]);
            }
        }
        printf("\n");
    }
} 

C. Woodcutters

Little Susie listens to fairy tales before bed every day. Today’s fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.

There are n trees located along the road at points with coordinates x1, x2, …, xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn’t contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.

Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of trees.

Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree.

The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.

Output
Print a single number — the maximum number of trees that you can cut down by the given rules.

Sample test(s)
input
5
1 2
2 1
5 10
10 9
19 1
output
3
input
5
1 2
2 1
5 10
10 9
20 1
output
4
Note
In the first sample you can fell the trees like that:

fell the 1-st tree to the left — now it occupies segment [ - 1;1]
fell the 2-nd tree to the right — now it occupies segment [2;3]
leave the 3-rd tree — it occupies point 5
leave the 4-th tree — it occupies point 10
fell the 5-th tree to the right — now it occupies segment [19;20]
In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].

  • DP,尽快搞出来。

D. Queue

Little girl Susie went shopping with her mom and she wondered how to improve service quality.

There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.

Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.

Input
The first line contains integer n (1 ≤ n ≤ 105).

The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.

Output
Print a single number — the maximum number of not disappointed people in the queue.

Sample test(s)
input
5
15 2 1 5 3
output
4
Note
Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.

  • 求最小等待的人数,其实拍完序之后,从小到大遍历,如果时间超过了,其实放在最后面也是没关系的,所以这题也变得非常的简单,sort。
#include <stdio.h>
#include <algorithm>

using namespace std;

int tt[100010];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; i ++)
        {
            scanf("%d",&tt[i]);
        }
        sort(tt, tt + n);
        long int sum = 0;
        int cnt = 0;
        for(int i = 0; i < n; i ++)
        {
            if(tt[i] >= sum)
            {
                cnt ++;
                sum += tt[i];
            }
        }
        printf("%d\n",cnt);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值