ABC 268C - Chinese Restaurant

文章描述了一个围绕转盘坐的编号问题,目标是通过有限次逆时针旋转转盘使得最多的人感到高兴。每个位置的人高兴的条件是特定的菜位于他、前一个或后一个位置。文章提供了不同的解题思路,包括记录每次转盘操作影响的人数并求最大值,以及直接计算每个位置的最大可能高兴人数。
摘要由CSDN通过智能技术生成

abc268

题意:

N N N 个人从 0 0 0 开始编号, 按逆时针顺序间隔均匀地坐在转盘周围。 在开始时, 第 p i p_{i} pi 盘菜在第 i i i 个人的前面。

现在, 你可以进行以下操作 0 0 0 次或多次。

  • 将转盘逆时针旋转 1 N \frac{1}{N} N1 圈。也就是说, 旋转前在第 i i i 号人面前的盘子现在在 ( i + 1 )   m o d   N (i+1)\bmod N (i+1)modN 号人面前了。

当你结束操作后,如果第 i i i 盘菜在第 ( i − 1 )   m o d   N (i-1)\bmod N (i1)modN 个人、第 i i i 个人或第 ( i + 1 )   m o d   N (i+1)\bmod N (i+1)modN 个人面前,第 i i i 个人就会感到高兴。

请求出你最多能使多少人感到高兴。

思路:

因为让第 i i i个人开心开心要把第 i i i盘菜转到 i − 1 i-1 i1, i i i, i + 1 i+1 i+1
那么我们就记 f i f_i fi表示转 i i i次开心的人数。
那么我们记第 i i i盘菜转到 i − 1 i-1 i1, i i i, i + 1 i+1 i+1的步数分别记为 c n t 1 cnt1 cnt1, c n t 2 cnt2 cnt2, c n t 3 cnt3 cnt3,那么 c n t c n t 1 + 1 , c n t c n t 2 + 1 , c n t c n t 3 + 1 cnt_{cnt1}+1, cnt_{cnt2}+1, cnt_{cnt_3}+1 cntcnt1+1,cntcnt2+1,cntcnt3+1,因为转 c n t 1 cnt1 cnt1, c n t 2 cnt2 cnt2, c n t 3 cnt3 cnt3次都可以让 i i i开心。
最后我们枚举转多少次(因为转 n n n次就转回来了,所以枚举到 n n n就足够了),然后对 c n t cnt cnt求个最大值就可以了。

注意:取模会有负数要特判一下( i − 1 i-1 i1)

My   Code: \texttt{My Code:} My Code:

#include <bits/stdc++.h>
using namespace std;
int n;
int a[200010], cnt[200010];
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i) {
		cnt[(a[i] - 1 - i - 1 + n) % n]++;//转到i-1
		cnt[(a[i] - 1 - i + n) % n]++;//转到i
		cnt[(a[i] - i + n) % n]++;//转到i+1
	}
	int ans = 0;
	for (int i = 0; i < n; ++i)
		ans = max(ans, cnt[i]);
	printf("%d\n", ans);
	return 0;
}

S08577 \texttt{S08577} S08577 Code \texttt{Code} Code:

这个 c n t i cnt_i cnti表示转 i i i次,正好转到面前的个数。
那么最后统计答案的就要在少转或多转一次(因为若和他想等的菜在 i + 1 i+1 i+1 i − 1 i-1 i1,那么在少/多转一次就能转到)

#include<iostream>
#include<cstring>
using namespace std;
int a[200010],b[200010],c[200010];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        int t=a[i]-i;
        if(t>=0) b[i]=t;
        else b[i]=t+n;
        c[b[i]]++;
    }
    int maxn=0;
    for(int i=1;i<n;i++){
        maxn=max(maxn,c[i-1]+c[i]+c[i+1]);
    }
    maxn=max(maxn,c[0]+c[1]+c[n-1]);
    maxn=max(maxn,c[n-1]+c[n-2]+c[0]);
    cout<<maxn;
    return 0;
}

addnine \texttt{addnine} addnine Code \texttt{Code} Code

这里用max_element求得最大值。

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
using ll = long long;
using ld = long double;

int main() {
    int N;
    cin >> N;
    vector<int> p(N), q(N);
    for (int i = 0; i < N; i++) cin >> p[i];
    vector<int> cnt(N, 0);
    for (int i = 0; i < N; i++) {
        int x = (p[i] + N - i) % N;
        cnt[(x-1+N) % N]++;
        cnt[x]++;
        cnt[(x+1) % N]++;
    }
    cout << *max_element(all(cnt)) << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值