• 2020ICPC南京站


    K

    K Co-prime Permutation

    题意:给定n和k,让你构造n的排列,满足gcd(pi, i)=1的个数为k。

    思路:因为x和x-1互质,1和任何数互质,任何数和它本身不互质

    当k为奇数时,p1=1,后面k-1个数两两互换

    当k为偶数时,后面k个数两两互换

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. typedef long long ll;
    5. const int N=1e6+10;
    6. const int inf=0x3f3f3f3f;
    7. using namespace std;
    8. int n,k;
    9. int a[N];
    10. void solve()
    11. {
    12. cin>>n>>k;
    13. if(k==0)
    14. {
    15. cout<<-1<<'\n';
    16. return ;
    17. }
    18. int cnt=0;
    19. for(int i=1;i<=n;i++) a[i]=i;
    20. if(k&1)
    21. {
    22. cnt=1;
    23. for(int i=2;i<=n&&cnt
    24. {
    25. if(cnt&1) a[i]=i+1;
    26. else a[i]=i-1;
    27. cnt++;
    28. }
    29. }
    30. else
    31. {
    32. for(int i=1;i<=n&&cnt
    33. {
    34. if(cnt%2==0) a[i]=i+1;
    35. else a[i]=i-1;
    36. cnt++;
    37. }
    38. }
    39. for(int i=1;i<=n;i++)
    40. cout<" \n"[i==n];
    41. }
    42. signed main()
    43. {
    44. //freopen("input.txt","r",stdin);
    45. //freopen("output.txt","w",stdout);
    46. ios;
    47. int _t=1;
    48. // cin>>_t;
    49. while(_t--) solve();
    50. system("pause");
    51. return 0;
    52. }

    L

    Let's Play Curling

    题意:给定n块红色石头,m块蓝色石头的位置。记红色石头的位置为a[i],蓝色石头的位置为b[i]。当红色石头到目标位置c的距离比蓝色所有石头到目标位置的距离都要小时,计一分,找到一个c点可以让红队尽可能多赢,输出红队尽可能多赢的次数。

    思路:在两块蓝色石头之间一定存在一个位置满足条件,得分为两个蓝色石头之间红色石头的个数。

    即求两个蓝色石头之间最多有几个红色石头。

    排序后枚举蓝色石头的位置p,二分红色石头找到上下界。

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. typedef long long ll;
    5. const int N=1e6+10;
    6. const int inf=0x3f3f3f3f;
    7. using namespace std;
    8. int n,m;
    9. void solve()
    10. {
    11. cin>>n>>m;
    12. vector<int>a,b;
    13. for(int i=1;i<=n;i++)
    14. {
    15. int x;
    16. cin>>x;
    17. a.push_back(x);
    18. }
    19. for(int i=1;i<=m;i++)
    20. {
    21. int x;
    22. cin>>x;
    23. b.push_back(x);
    24. }
    25. b.push_back(0);
    26. b.push_back(1e9+10);
    27. sort(a.begin(),a.end());
    28. sort(b.begin(),b.end());
    29. int ans=0;
    30. for(int i=0;i<=m;i++)
    31. {
    32. int l=upper_bound(a.begin(),a.end(),b[i])-a.begin();
    33. int r=lower_bound(a.begin(),a.end(),b[i+1])-a.begin();
    34. ans=max(ans,r-l);
    35. }
    36. if(ans==0) cout<<"Impossible\n";
    37. else cout<'\n';
    38. }
    39. signed main()
    40. {
    41. //freopen("input.txt","r",stdin);
    42. //freopen("output.txt","w",stdout);
    43. ios;
    44. int _t=1;
    45. cin>>_t;
    46. while(_t--) solve();
    47. system("pause");
    48. return 0;
    49. }

    E

    Evil Coordinate

    题意:初始位置为(0, 0),给定陷阱位置(x, y)和操作字符串。让我们重排列操作字符串使得不陷入陷阱。

    思路:设最终位置为(X, Y)若有解则(X, Y)与(x, y)至少有一维坐标不同,我们可以先走不同的那个方向,再走相同的那个方向。所以我们可以将相同操作排在一起,然后枚举UDLR的全排列就可以。

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. typedef long long ll;
    5. const int N=1e6+10;
    6. const int inf=0x3f3f3f3f;
    7. using namespace std;
    8. int x,y;
    9. string s;
    10. int dir[4][2]={0,1,0,-1,-1,0,1,0};
    11. char op[4]={'U','D','L','R'};
    12. map<int,int>cnt;
    13. string ans;
    14. bool check(vector<int>v)
    15. {
    16. ans.clear();
    17. int X=0,Y=0;
    18. for(int i=0;i<4;i++)
    19. {
    20. for(int j=0;j
    21. {
    22. ans+=op[v[i]];
    23. X+=dir[v[i]][0];
    24. Y+=dir[v[i]][1];
    25. if(X==x&&Y==y) return 0;
    26. }
    27. }
    28. return 1;
    29. }
    30. void solve()
    31. {
    32. cin>>x>>y;
    33. cin>>s;
    34. if(x==0&&y==0)
    35. {
    36. cout<<"Impossible\n";
    37. return ;
    38. }
    39. cnt.clear();
    40. for(int i=0;ilength();i++)
    41. if(s[i]=='U') cnt[0]++;
    42. else if(s[i]=='D') cnt[1]++;
    43. else if(s[i]=='L') cnt[2]++;
    44. else cnt[3]++;
    45. vector<int>v={0,1,2,3};
    46. bool f=0;
    47. do
    48. {
    49. if(check(v))
    50. {
    51. f=1;
    52. break;
    53. }
    54. } while (next_permutation(v.begin(),v.end()));
    55. if(!f)
    56. {
    57. cout<<"Impossible\n";
    58. return ;
    59. }
    60. else cout<'\n';
    61. }
    62. signed main()
    63. {
    64. //freopen("input.txt","r",stdin);
    65. //freopen("output.txt","w",stdout);
    66. //ios;
    67. int _t=1;
    68. cin>>_t;
    69. while(_t--) solve();
    70. system("pause");
    71. return 0;
    72. }

    F

    Fireworks

    题意:小明做一个烟花花费n的时间,点燃所有做好的烟花花费m的时间。每个烟花有p*10^{-4}的概率是完美的。求最优策略下最小时间花费。

    思路:假设最优策略是每生产k个再一起点燃,那么释放一次成功的概率为1-(1-p)^k  (p=p*1e-4).

    释放几次后得到完美的期望满足几何分布。

    几何分布:在n次伯努利试验中, 试验k次才得到第一次成功的概率。详细的说,是:前k-1次皆失败, 第k次成功的概率。 期望E(x)=1/p;(概率论公式,不再赘述)

    那么答案为E(x)*(nk+m)= (nk+m) / [1-(1-p)^k]

    接下来三分寻找答案的最小值。

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. typedef long long ll;
    5. const int N=1e6+10;
    6. const int inf=0x3f3f3f3f;
    7. using namespace std;
    8. double n,m;
    9. double p;
    10. double qmi(double a,int k)
    11. {
    12. double ret=1;
    13. while(k)
    14. {
    15. if(k&1) ret=ret*a;
    16. k>>=1;
    17. a=a*a;
    18. }
    19. return ret;
    20. }
    21. double get(int k)
    22. {
    23. double t=1.0-qmi(1.0-p,k);
    24. if(t==0) return (double)0x3f3f3f3f;
    25. return (k*n*1.0+m)/t;
    26. }
    27. void solve()
    28. {
    29. cin>>n>>m>>p;
    30. p=p*1e-4;
    31. double ans=(double)0x3f3f3f3f3f3f3f3f;
    32. int l=1,r=1e9;
    33. while(r>l)
    34. {
    35. int lmid=l+(r-l)/3,rmid=r-(r-l)/3;
    36. double f1=get(lmid),f2=get(rmid);
    37. ans=min(ans,min(f1,f2));
    38. if(f1-1;
    39. else l=lmid+1;
    40. }
    41. printf("%.10f\n",ans);
    42. }
    43. signed main()
    44. {
    45. //freopen("input.txt","r",stdin);
    46. //freopen("output.txt","w",stdout);
    47. //ios;
    48. int _t=1;
    49. cin>>_t;
    50. while(_t--) solve();
    51. system("pause");
    52. return 0;
    53. }

  • 相关阅读:
    手把手带你使用JWT实现单点登录
    刷题记录:牛客NC24263[USACO 2018 Feb G]Directory Traversal
    机器学习之 Jupyter Notebook 使用
    JavaScript【字符串数组实操、二维数组转化一维数组 、数组去重、数组排序、 函数概述、函数的重复声明、 函数名的提升、 函数的属性和方法、函数作用域、函数参数】(七)
    【ITRA】2022年ITRA赛事注册流程 从0-1
    [C++随想录] 二叉搜索树
    零时科技 || BXH攻击事件分析
    《动手学深度学习 Pytorch版》 9.6 编码器-解码器架构
    一次清理全屋地面,一键清洁烘干无异味,KEEWOO启为C260 Pro洗地机上手
    编程基础---C/C++基础知识
  • 原文地址:https://blog.csdn.net/qq_62615329/article/details/132630329