CodeForces 1015A-B-C-D-E-F

本文详细介绍了CodeForces 1015竞赛中的A到D题,包括题目大意、解题思路和代码实现。通过实例分析了如何在限制条件下找到不位于任何区间的整数、如何通过交换字符得到目标字符串、在有限次操作内完成歌曲压缩以及如何在指定步数内走完特定距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codefoces 1015

A - Points in Segments CodeForces - 1015A

You are given a set of n segments on the axis Ox, each segment has integer endpoints between 1 and m inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers li and ri (1≤li≤ri≤m) — coordinates of the left and of the right endpoints.

Consider all integer points between 1 and m inclusive. Your task is to print all such points that don’t belong to any segment. The point x belongs to the segment [l;r] if and only if l≤x≤r.

Input

The first line of the input contains two integers n and m (1≤n,m≤100) — the number of segments and the upper bound for coordinates.

The next n lines contain two integers each li and ri (1≤li≤ri≤m) — the endpoints of the i-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that li=ri, i.e. a segment can degenerate to a point.

Output

In the first line print one integer k — the number of points that don’t belong to any segment.

In the second line print exactly k integers in any order — the points that don’t belong to any segment. All points you print should be distinct.

If there are no such points at all, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.

Examples

Input
3 5
2 2
1 2
5 5
Output
2
3 4
Input
1 7
1 7
Output
0

Note

In the first example the point 1 belongs to the second segment, the point 2 belongs to the first and the second segments and the point 5 belongs to the third segment. The points 3 and 4 do not belong to any segment.

In the second example all the points from 1 to 7 belong to the first segment.

题目大意:

给一个n,一个m,然后给n个区间,
最后找到在1~m之间没有在给的那n个区间之间的点

具体做法:

直接暴力,给出n个区间的赋1
,最后为零的就是答案

代码如下
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
 
int main()
{
	int n, m, i, j, l, r, c = 0;
	scanf("%d%d", &n, &m);
	int ar[105] = { 0 };
	for (i = 0; i<n; i++) 
	{
		scanf("%d%d", &l, &r);
		for (j = l; j <= r; j++) 
		{
			ar[j] = 1;
		}
 
	}
 
	for (i = 1; i <= m; i++) 
	{
		if (!ar[i])
		{
			c++;
		}
	}
	printf("%d\n", c);
	for (i = 1; i <= m; i++) 
	{
		if (!ar[i])
		{
			printf("%d ", i);
		}
	}
	printf("\n");
	return 0;
}

B - Obtaining the String CodeForces - 1015B

You are given two strings s and t. Both strings have length n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 to n.

You can successively perform the following move any number of times (possibly, zero):

swap any two adjacent (neighboring) characters of s (i.e. for any i={1,2,…,n−1} you can swap si and si+1).
You can’t apply a move to the string t. The moves are applied to the string s one after another.

Your task is to obtain the string t from the string s. Find any way to do it with at most 104 such moves.

You do not have to minimize the number of moves, just find any sequence of moves of length 104 or less to transform s into t.

Input

The first line of the input contains one integer n (1≤n≤50) — the length of strings s and t.

The second line of the input contains the string s consisting of n lowercase Latin letters.

The third line of the input contains the string t consisting of n lowercase Latin letters.

Output

If it is impossible to obtain the string t using moves, print “-1”.

Otherwise in the first line print one integer k — the number of moves to transform s to t. Note that k must be an integer number between 0 and 104 inclusive.

In the second line print k integers cj (1≤cj<n), where cj means that on the j-th move you swap characters scj and scj+1.

If you do not need to apply any moves, print a single integer 0 in the first line and either leave the second line empty or do not print it at all.

Examples

Input
6
abcdef
abdfec
Output
4
3 5 4 5
Input
4
abcd
accd
Output
-1

Note

In the first example the string s changes as follows: “abcdef” → “abdcef” → “abdcfe” → “abdfce” → “abdfec”.

In the second example there is no way to transform the string s into the string t through any allowed moves.

题目大意:

给你两个字符串,然后如何变换a串中元素的位置,换成b串,注意只能每次交换相邻位置的a串,然后输出需要交换多少次,并且打印出每次交换的位置编号

思路:

暴力
for(int i = 0; i < N; i ++) {
mp[A[i]] ++;
mp[B[i]] --;
}
用这种方法判断是否两个串无法变换

代码如下
#include <bits/stdc++.h>
using namespace std;
 
int N;
char A[55], B[55];
vector<int> T;
map<char, int> mp;
 
int main() {
    mp.clear();
    scanf("%d", &N);
    scanf("%s%s", A, B);
    for(int i = 0; i < N; i ++) {
        mp[A[i]] ++;
        mp[B[i]] --;
    }
    for(int i = 0; i < N; i ++) {
        if(mp[A[i]]) {
            printf("-1\n");
            return 0;
        }
    }
    for(int i = 0; i < N; i ++) {
        for(int j = i; j < N; j ++) {
            if(B[i] == A[j]) {
                for(int k = j; k > i; k --) {
                    swap(A[k], A[k - 1]);
                    T.push_back(k);
                }
                break;
            }
        }
    }
 
    printf("%d\n", T.size());
    for(int i = 0; i < T.size(); i ++) {
        printf("%s", i != 0 ? " " : "");
        printf("%d", T[i]);
    }
    printf("\n");
    return 0;
}

C - Songs Compression CodeForces - 1015C

Ivan has n songs on his phone. The size of the i-th song is ai bytes. Ivan also has a flash drive which can hold at most m bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all n songs to the flash drive. He can compress the songs. If he compresses the i-th song, the size of the i-th song reduces from ai to bi bytes (bi<ai).

Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most m. He can compress any subset of the songs (not necessarily contiguous).

Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to m).

If it is impossible to copy all the songs (even if Ivan compresses all the songs), print “-1”. Otherwise print the minimum number of songs Ivan needs to compress.

Input

The first line of the input contains two integers n and m (1≤n≤105,1≤m≤109) — the number of the songs on Ivan’s phone and the capacity of Ivan’s flash drive.

The next n lines contain two integers each: the i-th line contains two integers ai and bi (1≤ai,bi≤109, ai>bi) — the initial size of the i-th song and the size of the i-th song after compression.

Output

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print “-1”. Otherwise print the minimum number of the songs to compress.

Examples

Input
4 21
10 8
7 4
3 1
5 4
Output
2
Input
4 16
10 8
7 4
3 1
5 4
Output
-1

Note

In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to 8+7+1+5=21≤21. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal 8+4+3+5=20≤21. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to 10+4+3+5=22>21).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8+4+1+4=17>16.

题目大意:

有m大小的空间,有n个物品,第i个物品本来的大小为ai,压缩后的大小为bi,问你最少只需要压缩多少个物品,就能把这个n个物品放进m大小的空间

思路:

设置一个结构体,保存压缩前后的字节;
然后以差值(就是压缩前后的差值)排序,先换差值最大的,然后换差值小的。写着写着突然发现没有必要用结构体。直接一个数组保存就行。

代码如下
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+3;
struct point
{
    int a,b;
    int sub;
}p[maxn];
bool cmp(point m1,point m2)
{
    return m1.sub<m2.sub;
}
int main()
{
    int n,m;
    cin>>n>>m;
    int x,y;
    long long sum1=0,sum2=0;
    for(int i=1;i<=n;i++)
    {
        cin>>x>>y;
        sum1+=x;
        sum2+=y;
        p[i].a=x;
        p[i].b=y;
        p[i].sub=x-y;
    }
    if(sum1<=m)
        cout<<"0"<<endl;
    else if(sum2>m)
        cout<<"-1"<<endl;
    else
    {
        sort(p+1,p+1+n,cmp);
    int flag;
    
    for(int i=n;i>0;i--)
    {
        if((sum1-p[i].sub)<=m)
        {
            flag=i;
            break;
        }
        else
        {
            sum1-=p[i].sub;
        }
        
    }
    cout<<n-flag+1<<endl;
    }

    return 0;
}

D - Walking Between Houses CodeForces - 1015D

There are n houses in a row. They are numbered from 1 to n in order from left to right. Initially you are in the house 1.

You have to perform k moves to other house. In one move you go from your current house to some other house. You can’t stay where you are (i.e., in each move the new

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值