Educational Codeforces Round 18 (A+B+C)

A. New Bus Route
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, …, an. All coordinates are pairwise distinct.

It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn’t want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.

It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.

Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.

Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).

The second line contains n integer numbers a1, a2, …, an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.

Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.

Examples
input
4
6 -3 0 4
output
2 1
input
3
-2 0 2
output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance.
题意:找最小的差和出现的个数。
题解:排序即可。
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7;
int a[N];
map<int,int>mp;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    int mx=3e9;
    for(int i=2;i<=n;i++)
    {
        mp[a[i]-a[i-1]]++;
        if(a[i]-a[i-1]<mx)
            mx=a[i]-a[i-1];
    }
    cout<<mx<<" "<<mp[mx]<<endl;
}

B. Counting-out Rhyme
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.

For example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and ai = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.

You have to write a program which prints the number of the child to be eliminated on every step.

Input
The first line contains two integer numbers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1).

The next line contains k integer numbers a1, a2, …, ak (1 ≤ ai ≤ 109).

Output
Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.

Examples
input
7 5
10 4 11 4 1
output
4 2 5 6 1
input
3 2
2 5
output
3 2
Note
Let’s consider first example:

In the first step child 4 is eliminated, child 5 becomes the leader.
In the second step child 2 is eliminated, child 3 becomes the leader.
In the third step child 5 is eliminated, child 6 becomes the leader.
In the fourth step child 6 is eliminated, child 7 becomes the leader.
In the final step child 1 is eliminated, child 3 becomes the leader.
题意:n个人占成一圈,每点ai个人淘汰一个,输出淘汰顺序
题解:queue应用,注意ai比较大,对人数取余。
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7;
int a[N];
queue<int>q;
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=k;i++) cin>>a[i];
    for(int i=1;i<=n;i++) q.push(i);
    for(int i=1;i<=k;i++)
    {
        int cnt=n-i+1;
        a[i]%=cnt;
        while(a[i]--)
        {
            int tmp=q.front();
            q.pop();
            q.push(tmp);
        }
        cout<<q.front()<<" ";
        q.pop();
    }
}

C. Divide by Three
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.

The number is called beautiful if it consists of at least one digit, doesn’t have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.

Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don’t have to go one after another in the number n.

If it’s impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

Input
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

Output
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

Examples
input
1033
output
33
input
10
output
0
input
11
output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.
题意:给出一个正整数n,问用最少的删除操作是它变成美丽数。
美丽数的定义:没有前导0,且是3的倍数。输出任意美丽数,无解输出-1。
题解:是三的倍数,和也是三的倍数。求和mod3之后,有三种情况,
1:和为0,即本身就是美丽数,直接输出。
2:和为1,此时应该删除一个1(mod3为1,下同)或者两个2。
3:和为2,此时应该删除一个2或者两个1。
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7;
string str,op;
int cnt[4];
int len;
void solo(string a)
{
    if(a.length()>op.length())
        op=a;
}
void delone(int x)
{
    if(cnt[x]<1) return ;
    string a;
    bool flag=true;
    for(int i=len-1;i>=0;i--)
    {
        if((str[i]-'0')%3!=x) a.push_back(str[i]);
        else
        {
            if(flag==false) a.push_back(str[i]);
            else flag=false;
        }
    }
    while(a.size()>1&&a.back()=='0') a.pop_back();
    reverse(a.begin(),a.end());
    solo(a);
}
void deltwo(int x)
{
    if(cnt[x]<2) return ;
    string a;
    int tmp=2;
    for(int i=len-1;i>=0;i--)
    {
        if((str[i]-'0')%3!=x) a.push_back(str[i]);
        else
        {
            if(tmp==0) a.push_back(str[i]);
            else tmp--;
        }
    }
    while(a.size()>1&&a.back()=='0') a.pop_back();
    reverse(a.begin(),a.end());
    solo(a);

}
int main()
{
    cin>>str;
     len=str.length();
    int ans=0;
    for(int i=0;i<len;i++)
    {
        ans=(ans+(str[i]-'0'))%3;
        cnt[(str[i]-'0')%3]++;
    }
    if(ans==0)
    {
        cout<<str<<endl;
        return 0;
    }
    else if(ans==1)
    {
        delone(1);
        deltwo(2);
    }
    else
    {
        delone(2);
        deltwo(1);
    }
    if(op.empty()) cout<<"-1"<<endl;
    else cout<<op<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值