CF 361 Div.2

A. Mike and Cellphone

题目

While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:

Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number “586” are the same as finger movements for number “253”:

Mike has already put in a number by his “finger memory” and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there any other number, that has the same finger movements?
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 9) — the number of digits in the phone number that Mike put in.
The second line contains the string consisting of n digits (characters from ‘0’ to ‘9’) representing the number that Mike put in.
Output
If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print “YES” (without quotes) in the only line.
Otherwise print “NO” (without quotes) in the first line.
Examples
input
3
586
output
NO
input
2
09
output
NO
input
9
123456789
output
YES
input
3
911
output
YES
Note
You can find the picture clarifying the first sample case in the statement above.

题目大意

给定一串字符,按着顺序依次连起来,成为一个图形,看其是否是唯一确定的(判断其是否有等价图形)

解题思路

① 直接枚举不就得了~(但要控制好条件,不方便写)

② 观察每一个数对应的特殊情况,比如 有1 的时候,出现了 9 则一定是唯一确定的。在思考为什么 1 和 9 可以唯一确定?假设有有一个 x 可以替代 1 的位置,则有 x 一定处在 1 的下边 或者 右边,在假设有一个 y 有可以 代替 9 的位置,则有 y 一定处在 9 的上边 或者 左边,肯定不能同时满足同时满足 x,y;(因为这些需求要同时满足,即是取交集,交集的方向 即是可以通过平移之后得到另一组等价图形的 方向);在比如 1,5,3(可以由 4,8,6代替)这是为什么呢?很简单 1 需要向下或者向右寻找,5嘛 上,下,左,右都需要,3 的话需要向下或者向左,然后取交集可得到是向下,也即是说 整体向下平移后可以得到等价图形(4,8,6;7,0,9都是通过向下平移得到的)

③ 根据②逆向来想,1,9为什么不可以等价替换,因为1 不可以在向上,向左移动,而9 又不可以向下,向右移动,也即是说整个图形不能向上下左右四个方向移动,,既然这样的话,那又怎么找得到替代呢;同理1 5 3 ,整体不能向上,向左,向右,但可以向下。这就是通过逆向思维来找到交集.

代码

// An highlighted block
#include<bits/stdc++.h>
using namespace std;
char s;
int l, r, u, d, n, i;
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	l = r = u = d = 1;
	for(i = 1; i <= n; i++)
	{
		cin >> s;
		if(s == '1' || s == '2' || s == '3')  u = 0;
		if(s == '1' || s == '4' || s == '7')  l = 0;
		if(s == '3' || s == '6' || s == '9')  r = 0;
		if(s == '7' || s == '9')			  d = 0;
		if(s == '0')						d = l = r = 0;
	}
	if(l || r || u || d)	cout << "NO";
	else					cout << "YES";
	return 0;
} 

B. Mike and Shortcuts

单源最短路径

题目:

Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.
City consists of n intersections numbered from 1 to n. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number i to intersection j requires |i - j| units of energy. The total energyspent by Mike to visit a sequence of intersections p1 = 1, p2, …, pk is equal to units of energy.
Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly n shortcuts in Mike’s city, the ith of them allows walking from intersection i to intersection ai (i ≤ ai ≤ ai + 1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p1 = 1, p2, …, pk then for each 1 ≤ i < k satisfying pi + 1 = api and api ≠ pi Mike will spend only 1 unit of energy instead of |pi - pi + 1| walking from the intersection pi to intersection pi + 1. For example, if Mike chooses a sequence p1 = 1, p2 = ap1, p3 = ap2, …, pk = apk - 1, he spends exactly k - 1 units of total energy walking around them.
Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequence p1 = 1, p2, …, pk = i.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000) — the number of Mike’s city intersection.
The second line contains n integers a1, a2, …, an (i ≤ ai ≤ n , , describing shortcuts of Mike’s city, allowing to walk from intersection i to intersection ai using only 1 unit of energy. Please note that the shortcuts don’t allow walking in opposite directions (from ai to i).
Output
In the only line print n integers m1, m2, …, mn, where mi denotes the least amount of total energy required to walk from intersection 1 to intersection i.
Examples
input
3
2 2 3
output
0 1 2
input
5
1 2 3 4 5
output
0 1 2 3 4
input
7
4 4 4 4 7 7 7
output
0 1 2 1 2 3 3
Note
In the first sample case desired sequences are:
1: 1; m1 = 0;
2: 1, 2; m2 = 1;
3: 1, 3; m3 = |3 - 1| = 2.
In the second sample case the sequence for any intersection 1 < i is always 1, i and mi = |1 - i|.
In the third sample case — consider the following intersection sequences:
1: 1; m1 = 0;
2: 1, 2; m2 = |2 - 1| = 1;
3: 1, 4, 3; m3 = 1 + |4 - 3| = 2;
4: 1, 4; m4 = 1;
5: 1, 4, 5; m5 = 1 + |4 - 5| = 2;
6: 1, 4, 6; m6 = 1 + |4 - 6| = 3;
7: 1, 4, 5, 7; m7 = 1 + |4 - 5| + 1 = 3.

题目大意

有一个城市有 n 个路口, 线性链接(1 --> 2 --> 3 --> … --> n)且距离为 1 ,当然这些边是双向的,可以互相到达,现在 每一个 路口都有一个捷径(shortcuts) 是 当前路口到达 另外一个路口的距离为 1 ,(由题意这条shortcuts 是单向)求从 1 到每一个点的最短距离.

核心思想 :

SPFA + 图的储存

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int N = 2000007;

struct way
{
    int v,w,next;
};

way e[N];
int head[N],cnt;
void add(int u,int v,int w)
{
    e[cnt].v=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt++;
}

int n,m,s,dist[N],i,u,v,w;

struct cmp
{
    bool operator()(int a,int b)
    {
        return dist[a]>dist[b];
    }
};

priority_queue<int,vector<int>,cmp> q;
int in[N];
void spfa()
{
    q.push(1);
    memset(dist,0x7f,sizeof(dist));
    dist[1]=0;
    int x;
    while(!q.empty())
    {
        x=q.top();q.pop();in[x]=0;
        for(i=head[x];i!=-1;i=e[i].next)
          if(dist[x]+e[i].w<dist[e[i].v])
          {
            dist[e[i].v]=dist[x]+e[i].w;
            if(!in[e[i].v])
              q.push(e[i].v),in[e[i].v]=1;
          }
    }
}

int main()
{
	int x;
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
	{
       scanf("%d",&x);
	   if(x != i)	add(i,x,1);
	}
	for(int i = 2;i <=n; i++)
	{
		add(i-1,i,1);
		add(i,i-1,1);
	}
    spfa();
    for(int i = 1; i <= n; i++)
      printf("%d ",dist[i]);
    return 0;
}
Ps单向与双向边要分别存储

C. Mike and Chocolate Thieves

题目:

Bad news came to Mike’s village, some thieves stole a bunch of chocolates from the local factory! Horrible!

Aside from loving sweet things, thieves from this area are known to be very greedy. So after a thief takes his number of chocolates for himself, the next thief will take exactly k times more than the previous one. The value of k (k > 1) is a secret integer known only to them. It is also known that each thief’s bag can carry at most n chocolates (if they intend to take more, the deal is cancelled) and that there were exactly four thieves involved.

Sadly, only the thieves know the value of n, but rumours say that the numbers of ways they could have taken the chocolates (for a fixed n, but not fixed k) is m. Two ways are considered different if one of the thieves (they should be numbered in the order they take chocolates) took different number of chocolates in them.

Mike want to track the thieves down, so he wants to know what their bags are and value of n will help him in that. Please find the smallest possible value of n or tell him that the rumors are false and there is no such n.

Input
The single line of input contains the integer m (1 ≤ m ≤ 1015) — the number of ways the thieves might steal the chocolates, as rumours say.

Output
Print the only integer n — the maximum amount of chocolates that thieves’ bags can carry. If there are more than one n satisfying the rumors, print the smallest one.

If there is no such n for a false-rumoured m, print  - 1.
Examples
input
1
output
8
input
8
output
54
input
10
output
-1

题目大意

说实话我开始读了半个小时都没有读懂(没有反应过来,去纠结于它给出的样例),读懂了就发现挺水的

我换个故事来讲,
从前有四个小偷 甲、乙、丙、丁,这个盗贼团伙有个分赃的规定:假设 甲分到了 a ,那么乙、丙、丁会分别得到 ak、akk、akkk,并且 k (k > 1 && k 为 正整数)只有小偷自己心里清楚,也可以看出丁是老大。据警察对这个团伙的作案分析,发现,如果在 每一次 偷东西 的时侯丁都会(占个卜,抽个签之类的玄学活动)决定用 m 种 方式进行偷窃,每一次丁很贪心,每一次都想偷到 最多的数目 n,但丁又有强迫症,在 最多数目不超过 n’ 的情况下,偷窃方式 有 m’ 种,只有当m’ == m的时候 他们才会下手(毕竟丁是大哥嘛 ) ,当然其他三人肯定有不满了,于是三个臭皮匠,开始计算 他们老大 (丁) 在给出 m 之后,自己最多能得到多少。当然老大也不傻,也开始要你计算他自己到底能得到多少?(当强迫症犯了只能不偷了,即收入为 -1,玄学活动也是要钱钱的~~~)

解题思路:

仔细分析一下 这个 m 到底是个啥? 显然就是在 去 n = n’ 时,他们的盗窃方式(也是分赃方案)
举个例子: m = 8, n = 64(即 丁最多得到 64 ,亦即 akkk <= 64)
① 当 a = 1时,得到, k = 2,3,4(三种)
② 当 a = 2时,得到,k = 2,3(两种)
③ 当 a = 3时,得到,k = 2(一种)
④ 当 a = 4时,得到,k = 2(一种)
……(每一个都只有 k = 2 这一种)
⑧ 当 a = 8时,得到,k = 2(一种)
⑨ 当 a = 9时,得到,k = 1(因为 k > 1,所以 此时只有 零 种
总计 m’ = 3 + 2 + 6 = 11 种 != m 所以不能丁不能得到 64,只能另寻其他的 n’
经过一番尝试 发现 n’ = 54 时 m’ = m = 8
所以 丁最多就得到 54 .
在回过头来思考一下 为什么刚刚 a 只能取到 8? 也即是说 该怎么算出 a_max? 不难发现当n确定时, 1 <=a <= n / k
k*k,所以 没 取一个 k 都有一个对应的 a 的区间, 取完所有 的 k(满足题意)变可得到 每一个 a , 每一个 a 的和 就是 m’.
当 k = 2 时, a = 1,2……8(八种) 8 = 64/(2^3);
当 k = 3 时, a = 1,2(两种) 2 = 64/(3^3)
当 k = 4 时, a = 1 (一种) 1 = 64/(1^3)
现在已经可以求出 m’ 了,剩下的就是考虑如何确定 n’ 了。
如何求呢?为什么要这么求呢?
因为 n 是 一个 满足题意下的极值点 变可想到用二分答案 的 思路来确定 n.
如何操作?
给定一个 n’ ,便可计算出 m’
若(m’ < m) 说明 n 偏小,需要调大 , 则 L = mid + 1;
若(m’ > m) 说明 n 偏大,需要调小,则 R = mid - 1;
若 ( m’ == m)说明 n 可以了, 则继续找看是否有更大 的 n’ ,所以 R = mid
显然 后两种情况可以合并为 If(m’ >= m) R = mid;
到二分的最后时候肯定是 L == R,在判断一下 n= R 时的m’ ,是否满足 m’ == m;
若满足 则 就为 R,否则 为 -1

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll m, n, r, l, ans, mid,cnt;

ll judge(ll k)
{
	ans = 0;
	for(ll i = 2; i * i * i <= k; i++)
		ans += k / (i * i * i);
	return ans;
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> m;
	l = 1; r = 1e18;
	cnt = -1;
	while(l < r)
	{
		mid = (l + r) / 2;
		if(judge(mid) < m)	l = mid + 1;
		else    r = mid;
	}
	if(judge(r) == m)   cout << r;
	else    cout<<-1;
	return 0;
}

二分要注意到 退出二分的时候 的情况(这里既有可能出错,我在 m ==13 的时候就出了错)

D. Friends and Subsequences

题目:

Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?
Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of while !Mike can instantly tell the value of .
Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs is satisfied.
How many occasions will the robot count?
Input
The first line contains only integer n (1 ≤ n ≤ 200 000).
The second line contains n integer numbers a1, a2, …, an ( - 109 ≤ ai ≤ 109) — the sequence a.
The third line contains n integer numbers b1, b2, …, bn ( - 109 ≤ bi ≤ 109) — the sequence b.
Output
Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied.
Examples
input
6
1 2 3 2 1 4
6 7 1 2 3 2
output
2
input
3
3 3 3
1 1 1
output
0
Note
The occasions in the first sample case are:
1.l = 4,r = 4 since max{2} = min{2}.
2.l = 4,r = 5 since max{2, 1} = min{2, 3}.
There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.

题目大意:

给定两个数列 A , B 找到一个区间 (L,R)使得 max = ; min = ,且 max = min ,求这种区间的个数。

解题思路:

区间求最值可以联想到 “ST” 用 log2(N)来预处理出 A,B 的区间最值,所以的得到最朴素的算法:
Ps. A_max( i,r) ,表示 ,B_min(i , r ) 表示

	for(int i = 1; i <= n; i++)
		for(int r = i + 1; r <= n; r++)
			if(A_max(i,r) == B_min(i,r))	ans++;

可轻易的得出时间复杂度为 n^2,在分析时间复杂度主要集中在 找到 合适的 R 。为什么没有简便的方法求R? 在给定的一个L,如果有 R’ 、R’’ 使得 max = min 则 共有 R’’ – R’ + 1 个区间都满足题意。所以只需要固定 L,来求 R’,R’’。为什么要用R’,R’’? R’ 表示 满足题意的最小右端,R’’ 表示满足题意的最大右端,而L为 需要枚举的左端,动手画个图就了然了。然后呢?只需要两次二分就可以求出 R’,R’’.

代码(ST + 二分答案)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+7;
using namespace std;
const int INF = 0x7fffffff;
#define read(x)	scanf("%d",&x);
int minx[N][30],maxx[N][30];
int n;
typedef long long ll;
int log2(int x)
{
	int ans = 0;
	while(x>>1)
	{
		x >>= 1;
		ans++;
	}
	return ans;
}

int mmax(int l,int r)       // A_max
{
	int k=log2(r-l+1);
	
	return max(maxx[l][k],maxx[(r-(1<<k)+1)][k]);
}

int mmin(int l,int r)	  // B_min
{
	int k=log2(r-l+1);
	
	return min(minx[l][k],minx[(r-(1<<k)+1)][k]);
}
 
void init()              // 建立ST 表 
{
	for(int i = 1; i <= n; i++)
		for(int j = 0; j <= log2(n) + 1; j++)
			{
				minx[i][j] = INF;
				maxx[i][j] = -INF;
			}
	for(int i = 1; i <= n; i++)	read(maxx[i][0]);
	for(int i = 1; i <= n; i++)	read(minx[i][0]);
	for(int	j=1;j<=log2(n);j++)
		for(int i=1;i+(1<<j)-1<=n;i++)
		{
			minx[i][j]=min(minx[i][j-1],minx[i+(1<<(j-1))][j-1]);
			maxx[i][j]=max(maxx[i][j-1],maxx[i+(1<<(j-1))][j-1]);
		}
}
int main()
{
	read(n);
	init();
	int l, r, mid;
	ll ans = 0;
	int val1,val2;
	int ans_first = -1, ans_last = -1;
	for(int i = 1; i <= n; i++)
	{
		ans_first = -1, ans_last = -1;
		l = i; r = n;
		while(l <= r)
		{
			mid = (l + r) / 2;
			val1 = mmax(i,mid);
			val2 = mmin(i,mid);
			if(val1 == val2)	ans_first = mid;
			if(val1 >= val2)	r = mid - 1;
			else	l = mid + 1;
		}
		if(ans_first == -1)		continue;
		l = i;r = n;
		while(l <= r)
		{
			mid = (l + r) / 2;
			val1 = mmax(i,mid);
			val2 = mmin(i,mid);
			if(val1 > val2)	r = mid - 1;
			else	l = mid + 1, ans_last = mid;
		}
		ans += ll(ans_last - ans_first + 1);
	}
	printf("%lld",ans);
	return 0;
}
	第一个二分和第二个二分的差就在于要保证 两个R一定是两个最值(即最左端的 R,最右端的R)
	而且每一次枚举二分查找区间最值的时候,一定要不要忘记 是在固定L的基础上来找R' 和 R''
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值