C. Anonymous Informant-Codeforces Round 908 (Div. 2)

文章讨论了如何在一个给定数组中进行变换,通过选择每个位置的值作为固定点并移动数组元素,直到找到是否存在一种方式使得每次操作后固定点都在数组末尾。原始的暴力模拟方法因时间复杂度过高而超时,通过记录最后一个数的下标并用vis数组标记已访问,实现了优化算法。
摘要由CSDN通过智能技术生成

C. Anonymous Informant

题意就是给出一个数组,问是否存在一个数组经过变换得到该数组。
变换:a[i]=i的点可以作为固定点,确定这个固定点之后把数组往左移,左移的数移到数组末端。
例如:7 2 1 4 5
可以选择第二个点(a[2]==2符合条件)作为固定点
变化后的数组变为1 4 5 7 2
此时可以选择第一个点(a[1]==1符合条件)为固定点
以此类推…

那么可以发现每次移动后,选择的固定点总是在新的数组的最后一位,那么是不是每次选择最后一位数进行k次倒推即可呢?
答案不存在的条件就是最后一位数大于n,因为大于n的移动操作无法完成。
那么就有了以下的模拟代码:

/*错误代码,超时TLE*/
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main()
{
    int T;cin>>T;
    while(T--){
        int n,k;cin>>n>>k;
        vector<int>q;
        for(int i=1;i<=n;i++){
            int t;cin>>t;
            q.push_back(t);
        }
        map<vector<int>,int>mp;
        mp[q]=1;
        int f=1;
        while(k--){
            int t=q[n-1];
            if(t>n) {
                f=0;break;
            }
            /*关键模拟部分,对原数组进行k次操作,每次操作移动t个数*/
            while (t--)
            {
                q.insert(q.begin(),q[n-1]);
                q.erase(q.begin()+n);
            }
            /************************************************/
            if(mp[q]) break;
            mp[q]=1;
        }
        if(f==0) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
}

数据范围 n , k n, k n,k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105, 1 ≤ k ≤ 1 0 9 1 \le k \le 10^9 1k109)
很明显暴力模拟是会超时的,尽管使用了 map<vector,int>mp来优化当在操作过程中出现重复数组时结束程序,提交代码依旧是超时的。

优化:

保持数组不动,定义一个新的变量pos存放最后一个数的下标,那么原数组不动,只移动pos这样就能够提高运行效率。
判断是否重复也更简单了,只需要vis数组标记该点pos是否访问过即可。

#include<iostream>
#define int long long
using namespace std;
signed main()
{
    int T;cin>>T;
    while(T--){
        int n,k;cin>>n>>k;
        int q[n+1],vis[n+1]={0};
        for(int i=0;i<n;i++){
            cin>>q[i];
        }
        int f=1;
        int pos=n-1;
        while(k--){
            if(q[pos]>n){
                f=0;
                break;
            }
            if(vis[pos]) break;
            vis[pos]=1;
            pos=(pos-q[pos]+n)%n;
        }
        if(f==0) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
}
Sure! Here's your code with comments added: ```matlab F = zeros(length(z), 1); % Initialize the F vector with zeros for i = 1:length(z) % Define the Phi function using anonymous function Phi = @(theta, R, r) (z(i) + lc - lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) + lc - lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) - lc + lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) - lc + lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) + lc + lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) + lc + lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) - lc - lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.sin(theta)).sqrt(R.^2 + r.^2 + (z(i) - lc - lm).^2 - 2*R.*r.sin(theta))); % Calculate the value of F(i) using the integral3 function F(i) = BrNI / (4 * lc * (Rc - rc)) * integral3(Phi, 0, 2*pi, rc, Rc, rm, Rm); end ``` This code calculates the values of the vector `F` using a loop. The `Phi` function is defined as an anonymous function that takes `theta`, `R`, and `r` as input parameters. It performs a series of calculations and returns a value. The integral of `Phi` is then calculated using the `integral3` function. The result is stored in the corresponding element of the `F` vector. Please note that I have made some assumptions about the variables and functions used in your code since I don't have the complete context. Feel free to modify or clarify anything as needed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值