2020牛客国庆DAY5

C.Great Deceiver
题目:
链接:https://ac.nowcoder.com/acm/contest/7852/C
来源:牛客网

Once upon a time Baron Munchhausen traveled to the Moon. After that he often tells interesting stories about the Selenites. Recently Baron told us about their numeric system. They use a notation with negative radix!
Negative radix is quite hard for Humans, and even for Munchhausen. So, Baron did a trick to help himself on the Moon. He remembered all the numbers between 0 and n inclusively, which have the same notation for both Selenites’ negative radix −k and a more convenient positive radix k.
Munchhausen claims that he did that easily. But, you know, Baron can exaggerate a little. To catch him, you have to count how many numbers he must have remembered.
Note: the k-radix notation of a number x is a sequence of integers a0, a1, …, ap such that 0 ≤ ai < |k| and

题意:
给出N,K,问1到N之间的数转换成K进制和-K进制的数是一样的个数。

思路:
首先观察,如果±K进制表示都是一样的话,那说明在奇数次幂的位置应该都是0。长度若是奇数,那偶数次幂的位置就占一半,结果就是偶数次幂的数的总和。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const double N = 1e6+10;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 1000010
long long int a[MAXN];
int main()
{
	ll n, k;
    int i, j;
    scanf("%lld %lld", &n, &k);
    int Len = 0;
    while(n) a[Len++] = n % k, n /= k;
//        cout << Len << endl;
//        for(int i = 0 ; i < Len ; i++)
//        {
//        	cout << a[i] << "*" << endl;
//		}
    if(Len % 2 == 0)
    {
        ll ans = pow(k, Len/2);
        printf("%lld\n", ans);
//          cout << "****" << endl;
		return 0;
    }
    i = Len - 2;
    for(int flag = 1; i >= 1 && flag; i -= 2)
    {
        if(a[i])
        {
            for(j = i-1; j >= 0; j -=2)
            {
                a[j] = k-1; 
            }
            break;
        }
    }
    ll ans = 0, mul = 1;
    for(i = 0; i < Len; i += 2)
    {
        ans += mul * a[i];
        mul *= k;
    } 
    printf("%lld\n", ans+1);
    return 0;
}

H.Fraud Busters
题目:

链接:https://ac.nowcoder.com/acm/contest/7852/H
来源:牛客网

The number of cars in Default City that travel to the city center daily vastly exceeds the number of available parking spots. The City Council had decided to introduce parking fees to combat the problem of overspill parking on the city streets. Parking fees are enforced using an automated vehicle registration plate scanners that take a picture of the vehicle registration plate, recognize the sequence of digits and letters in the code on the plate, and check the code against a vehicle registration database to ensure that parking fees are dutifully paid or to automatically issue a fine to the vehicle owner otherwise.
As soon as parking fees were introduced, a parking fee fraud had appeared. Some vehicle owners had started to close one or several digits or letters on their vehicle registration plate with pieces of paper while they park, thus making it impossible for the current version of the automated scanner to recognize their vehicle’s registration code and to issue them a fine.
The Default City Council had instituted the Fraud Busters Initiative (FBI) to design a solution to prevent this kind of fraud. The overall approach that FBI had selected is to expand the number of vehicle features that scanners recognize (including features like vehicle type and color), as well as excluding from the list any vehicles that are detected to be elsewhere at this time. This information should help to identify the correct vehicle by narrowing down the search in the vehicle registration database.
You are working for FBI. Your colleagues had already written all the complex pieces of the recognition software that analyses various vehicle features and provides you with a list of registration codes that might potentially belong to a scanned car. Your task it to take this list and a recognized code from the license plate (which may be partially unrecognized) and find all the registration codes that match.

题意:
给出标准的字符串的格式,下面会给你一系列的字符串,输出满族形式的字符串。

思路:
直接模拟

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const double N = 1e6+10;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 1000010
string a[MAXN];
int main()
{
	string s;
	cin >> s;
	int n;
	cin >> n;
	int l1 = s.length();
	int res = 0;
	string x;
	int l2;
	for(int i = 0 ; i < n ; i++)
	{
		cin >> x;
		l2 = x.length();
		if(l1!=l2)
		{
			continue;
		}
		else
		{
			int flag = 0;
			for(int j = 0 ; j < l1 ; j++)
			{
				if(s[j]=='*')
				{
					continue;
				}
				else
				{
					if(s[j] == x[j])
					{
						continue;
					}
					else
					{
						flag = 1;
						break;
					}
				}
			}
			if(flag == 0)
			{
				a[res] = x;
				res++;
			}
		}
	}
	cout << res << endl;
	for(int i = 0 ; i < res ; i++)
	{
		cout << a[i] << endl;
	}
	return 0;
}

K.Knockout Racing
题目:
链接:https://ac.nowcoder.com/acm/contest/7852/K
来源:牛客网

The races became more popular than ever at Pandora planet. But these races are quite unusual. There are n cars participating in a race on the long straight track. Each car moves with a speed of 1 meter per second. Track has coordinates in meters.
The car number i moves between two points on the track with coordinates ai and bi starting at the second 0 in the point ai. The car moves from ai to bi, then from bi to ai, then from ai to bi again, and so on.
Handsome Mike wants to knock some cars out of the race using dynamite. Thus he has m questions. The question number j is: what is the number of cars in the coordinates between xj and yj inclusive after tj seconds from the start?
Your task is to answer Mike’s questions.

题意:
有很多段区间,每个区间中有小车以每秒一个单位的速度在行驶,从左到右再到左再到右一直循环往复。有个人想在t秒的时候炸掉一段路,问他能炸掉多少辆车。

思路:
直接模拟就行,在t的时间里对每个区间的二倍取模,然后在判断是否在炸掉的区间里就行了。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
const double N = 1e6+10;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const inline int read(){
    int k = 0, f = 1; char c = getchar();
    for(;!isdigit(c); c = getchar())
        if(c == '-') f = -1;
    for(;isdigit(c); c = getchar())
        k = k * 10 + c - '0';
    return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 10010
int n , m;
long long int a[MAXN];
long long int b[MAXN];
int ans[MAXN];
struct node
{
	long long int x;
	long long int y;
	long long int t;
}c[MAXN];
int main()
{
	cin >> n >> m;
	for(int i = 0 ;  i < n ; i++)
	{
		cin >> a[i] >> b[i];
	}
	for(int i = 0 ; i < m ; i++)
	{
		cin >> c[i].x >> c[i].y >> c[i].t;
	}
	for(int i = 0 ; i < m ; i++)
	{
		int k = 0;
		for(int j = 0 ; j < n ; j++)
		{
			long long int d;
			d = c[i].t % ((b[j] - a[j]) * 2);
            long long int pos;
			pos = a[j];
            if (pos + d <= b[j])    
				pos += d;
        	else    
				pos = b[j] - (d - (b[j] - a[j]));
            if (c[i].x <= pos && pos <= c[i].y)    
				k++;
		}
		ans[i] = k;
	}
	for(int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}
	return 0;
}

菜的一批的我就只写出来三道题,下面是补题:
B.Hyperdrome
题目:
链接:https://ac.nowcoder.com/acm/contest/7852/B
来源:牛客网

Hypergnome planet is famous for its Great Universal Games between gnomes — the Games between gnomes from each part of the galaxy in various disciplines.
The most popular discipline in the Games is the Hyperdrome discipline. The rules are the follows: one string of length n is given to all gnomes. The gnomes shall find, as fast as they can, the total number of Hyperdrome substrings — such strings that characters inside the string can be rearranged to get a palindrome.
Substring is defined as a sequence of characters from position i to position j inclusive, where 1 ≤ i ≤ j ≤ n. Substrings with different pairs of positions (i, j) are considered different regardless of their contents.
Palindrome is defined as a string x1x2…xl, where xi = xl−i+1 for all 1 ≤ i ≤ l.
Judges choose a string and your task is to help them find the answer.
The gnome alphabet consists of lowercase and uppercase English letters — ‘a’–‘z’ and ‘A’–‘Z’ where letters in different case are considered to be different letters.

题意:
找出所有可重排的回文串的数量。

思路:
当时在写的时候无论如何也过不了复杂度的限制,敲之前就直接否决了。看了网上dalao的代码与思路,大喊三遍这也tql。
https://blog.csdn.net/pbihao/article/details/54882154?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160194844019195188363431%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=160194844019195188363431&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v28_p-2-54882154.pc_first_rank_v2_rank_v28_p&utm_term=Hyperdrome&spm=1018.2118.3001.4187

首先假设内存足够大的话可以用f[i]表示i这个状态出现了多少次,状态是指异或前缀和,那么根据题意,当且只有在i0或者二进制表示下只有一个1的情况下是合法状态,所以每加一个字符,就更新当前前缀和,又由于AB==C->ACB

那么可以预处理出所有合法状态,然后分别和当前状态异或后得到需要的状态,加入答案。

但是内存不够,不能用数组,amp貌似是一个不错的选择,呵呵,又T又mle,那么hash好了,呵呵,冲突太多,所以正解是用一个链表来处理所有的冲突,汗。。。

然后到底hash的时候取模多少呢?好像只要是一个不爆内存的数就好了,呵呵,卡long long取模,解决办法是选8388607因为它的二进制数是11111111111111111111111就可以用&来代替%了,嗯,我服

但是代码并不长

代码:

#include
#include
#include
#define Mod 8388607
#define maxn 3*100021
using namespace std;
typedef long long LL;
int head[Mod+4],n,tot=1;
LL pre[53],ans;
char s[maxn];
inline int Q(char c){return c>='a'&&c<='z' ? c-'a'+27 : c-'A'+1;}
struct edge{LL x;int next,cnt;}e[maxn];
void ins(LL x){
	int y=x&Mod;
	for(int i=head[y];i;i=e[i].next)if(e[i].x==x){
		e[i].cnt++;return;
	}e[tot].x=x,e[tot].cnt=1,e[tot].next=head[y];
	head[y]=tot++;
}
int query(LL x){
	int y=x&Mod;
	for(int i=head[y];i;i=e[i].next)if(e[i].x==x)return e[i].cnt;
	return 0;
}
 
int main(){
	scanf("%d%s",&n,s+1);ins(0);
	for(int i=1;i<=52;i++)pre[i]=1ll<<i;LL sum=0;
	for(int x,i=1;i<=n;i++){
		x=Q(s[i]);sum^=pre[x];
		for(int i=0;i<=52;i++)
			ans+=query(sum^pre[i]);
		ins(sum);
	}
	cout<<ans;
	return 0;
}

你可能感兴趣的:(牛客)