codeforces round 946(div.3)

A:

题目描述:

Little Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size 5 × 3 5 \times 3 5×3, i.e., five rows and three columns.

There are x x x applications with an icon size of 1 × 1 1 \times 1 1×1 cells; such an icon occupies only one cell of the screen. There are also y y y applications with an icon size of 2 × 2 2 \times 2 2×2 cells; such an icon occupies a square of 4 4 4 cells on the screen. Each cell of each screen can be occupied by no more than one icon.

Rosie wants to place the application icons on the minimum number of screens. Help her find the minimum number of screens needed.

分析:

首先判断要放下所有的 2 × 2 2 \times 2 2×2的图标需要多少页,再判断这些页剩余部分有多少,与 1 × 1 1 \times 1 1×1的图标数进行判断。

代码:

#include<bits/stdc++.h>
using namespace std;
 
int main()
{
	int tt;
	cin >> tt;
	while(tt--)
	{
		int a,b;
		cin >> a >> b;
		int ans=(b+1)/2;
		a-=ans*15-b*4;
		if(a>0)
		{
			ans+=a/15;
			if(a%15>0)
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
 } 

B:

问题描述:

Polycarp has a string s s s, which consists of lowercase Latin letters. He encodes this string using the following algorithm:

  • first, he constructs a new auxiliary string r r r, which consists of all distinct letters of the string s s s, written in alphabetical order;
  • then the encoding happens as follows: each character in the string s s s is replaced by its symmetric character from the string r r r (the first character of the string r r r will be replaced by the last, the second by the second from the end, and so on).

For example, encoding the string s s s=“codeforces” happens as follows:

  • the string r r r is obtained as “cdefors”;
  • the first character s 1 s_1 s1=‘c’ is replaced by ‘s’;
  • the second character s 2 s_2 s2=‘o’ is replaced by ‘e’;
  • the third character s 3 s_3 s3=‘d’ is replaced by ‘r’;
  • the last character s 10 s_{10} s10=‘s’ is replaced by ‘c’.


The string r r r and replacements for s s s=“codeforces”.

Thus, the result of encoding the string s s s=“codeforces” is the string “serofedsoc”.

Write a program that performs decoding — that is, restores the original string s s s from the encoding result.

分析:

给定一个字符串,将字符串中不重复的字符按字典序排列,然后按照排序后的字符对称相连的方式,将原字符串中所有的字符进行替换,输出替换后的新字符。

代码1:

#include<bits/stdc++.h>
using namespace std;
const int N =2*1e5+10;
int main()
{int tt;
	cin >> tt;
	while(tt--)
	{
		char a[N];
        int n,m,a1[N],b[N]={0},c[30]={0},d[30]={0};
		cin >> n;
		m=n;
		cin >> a;
		for(int i=0;i<n;i++)
		{
			b[i]=a[i]-'a'; //将字符转换为数字,方便后续使用。
			a1[i]=b[i];
		}
		sort(b,b+n);
		for(int i=0,j=0;i<n;i++)
		{
		    if(b[i]!=b[i+1]){
		        c[j++]=b[i];
		    }
		    else m--;
		}
		for(int i=0;i<m;i++)
		{
			d[c[i]]=c[m-i-1];
		}
		for(int i=0;i<n;i++)  
		{
			printf("%c",'a'+d[a1[i]]);
		}
		cout << endl;
	}
	return 0;
} 

优化代码:

#include <bits/stdc++.h>
using namespace std;
 
int main() { 
    int tt;
    cin >> tt;
    while(tt--)
	{ 
        int n;
        cin >> n;
        string b;
        cin >> b;
        auto s = b;
        sort(s.begin(), s.end());
        s.erase(unique(s.begin(), s.end()), s.end());
        vector<int> a(26,-1);
        for (int i = 0; i < s.size(); i++)
        {
            a[s[i]-'a']=s[s.size()-1-i]-'a';
        }
        for(char q: b)
		{
            cout << char('a'+a[q-'a']);
        }
        cout << endl;
    }
    return 0;
}

C:

问题描述:

Polycarp was given an array a a a of n n n integers. He really likes triples of numbers, so for each j j j ( 1 ≤ j ≤ n − 2 1 \le j \le n - 2 1jn2) he wrote down a triple of elements [ a j , a j + 1 , a j + 2 ] [a_j, a_{j + 1}, a_{j + 2}] [aj,aj+1,aj+2].

Polycarp considers a pair of triples b b b and c c c beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:

  • b 1 ≠ c 1 b_1 \ne c_1 b1=c1 and b 2 = c 2 b_2 = c_2 b2=c2 and b 3 = c 3 b_3 = c_3 b3=c3;
  • b 1 = c 1 b_1 = c_1 b1=c1 and b 2 ≠ c 2 b_2 \ne c_2 b2=c2 and b 3 = c 3 b_3 = c_3 b3=c3;
  • b 1 = c 1 b_1 = c_1 b1=c1 and b 2 = c 2 b_2 = c_2 b2=c2 and b 3 ≠ c 3 b_3 \ne c_3 b3=c3.

Find the number of beautiful pairs of triples among the written triples [ a j , a j + 1 , a j + 2 ] [a_j, a_{j + 1}, a_{j + 2}] [aj,aj+1,aj+2].

分析:

满足下列条件任意一个的被称为完美三元组,求给定数组的子序列中有多少组。

分析:

循环给定数组,按照ai,ai+1,ai,ai+2,ai,ai+2,分别存入三个map中,存储此时相同的数量,再判断这中排列顺序是否出现过,如果出现过则需从答案中减去这部分值。

以第一组数据为例,3 2 2 2 3

map1(3,2)++,map2(3,2)++,map3(2,2)++,v(3,2,2)++
map1(2,2)++,map2(2,2)++,map3(2,2)++,v(2,2,2)++
map1(2,2)++,map2(2,3)++,map3(2,3)++,v(2,2,3)++

ans=+1+1-0=2;

代码:

#include<bits/stdc++.h>
using namespace std;
const int N =2*1e5+10;
int main()
{
	int tt;         
	cin >> tt;
	while(tt--)
	{
		int n,a[N];
		cin >> n;
		for(int i=0;i<n;i++)
		{
			cin >> a[i];
		}
		long long int ans=0;
		map<pair<int,int>,int> map1,map2,map3;
		map<vector<int>,int> cnt;
		for(int i=0;i+2<n;i++)
		{
			ans+=map1[{a[i],a[i+1]}]++;
			ans+=map2[{a[i],a[i+2]}]++;
			ans+=map3[{a[i+1],a[i+2]}]++;
			vector<int> v={a[i],a[i+1],a[i+2]}; 
			ans-=cnt[v]*3;
			cnt[v]++;
		}
		cout << ans << endl;
	}
	return 0;
}

D:

问题描述:

Let’s imagine the surface of Mars as an infinite coordinate plane. Initially, the rover Perseverance-2 and the helicopter Ingenuity-2 are located at the point with coordinates ( 0 , 0 ) (0, 0) (0,0). A set of instructions s s s consisting of n n n instructions of the following types was specially developed for them:

  • N: move one meter north (from point ( x , y ) (x, y) (x,y) to ( x , y + 1 ) (x, y + 1) (x,y+1));
  • S: move one meter south (from point ( x , y ) (x, y) (x,y) to ( x , y − 1 ) (x, y - 1) (x,y1));
  • E: move one meter east (from point ( x , y ) (x, y) (x,y) to ( x + 1 , y ) (x + 1, y) (x+1,y));
  • W: move one meter west (from point ( x , y ) (x, y) (x,y) to ( x − 1 , y ) (x - 1, y) (x1,y)).

Each instruction must be executed either by the rover or by the helicopter. Moreover, each device must execute at least one instruction. Your task is to distribute the instructions in such a way that after executing all n n n instructions, the helicopter and the rover end up at the same point, or determine that this is impossible.

分析:

分别定义两辆车为a,b,移动方向有四个,我们再这里只讨论N和S。

对于一定量的有NS字符构成的字符串,我们要把它平均分配给a,b两辆车,则我们可以对NS赋值,开始时N=S=0,当字符赋值为零时,我们把这个字符给a,同时改变该字符的赋值为1,当字符赋值为1时,我们把这个字符给b。最后判断两字符的赋值是否相同,若相同,则a,b两车到达的位置相同。

同理可证WE两个方向。

最后还需保证两辆车都必须移动过,则我们初定义NS的赋值为0,WE的赋值的赋值为1,赋值为0的字符给a车,赋值为1的字符给b车,尽量使两辆车都移动,最后再用一个条件判断是否两辆车都移动了。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N =2*1e5+10;
char s[N],ans[N];
int w[128];
int main()
{
	int tt;         
	cin >> tt;
	while(tt--)
	{
		int n;
		cin >> n;
		cin >> s+1;
		int ok=0;
		w['N']=w['S']=0;
		w['W']=w['E']=1;
		for(int i=1;i<=n;i++)
		{
			if(w[s[i]]==0)
			ans[i]='R';
			else
			ans[i]='H';
			if(ans[i]=='R') ok|=1; //0|1=1,1|1=1
			if(ans[i]=='H') ok|=2; //0|2=2,1|2=3
			w[s[i]]^=1;
		}
		ans[n+1]='\0';
		if(ok==3&&w['N']==w['S']&&w['W']==w['E']) //只有ok的值为3才说明两辆车都移动了
		puts(ans+1);
		else 
		puts("NO");
	}
	return 0;
}

24/5/28

  • 47
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值