NOIP赛前模拟20171027总结

题目:

1.寿司

  给定一个环形的RB串··要求经过两两互换后RB分别形成两段连续区域,求最少操作次数(算法时间O(n))

2.金字塔

  给定一个金字塔的侧面图有n层··已知每一层的宽度··高度均为1··要求在图中取出恰好K个互不相交的矩形(边缘可以重叠),求最多可以取出多少面积

  n<=20000,k<=100

3.心灵治愈

  给定n,m要求取出不大于m的n个正整数,问有多少种取法使n个数和m的最大公因数为1,n,m<=10^15

题解:

1.分析

  首先为了方便我们把环从中间断开看成一个序列,我们考虑如果移动R串··那么肯定是找到某一B为中心··B的左边R移到一起··B的右边R移到一起(这里描述有点模糊···如果序列左边的R都移到一起,且序列最左边为R,右边同理··则实际在环中R肯定是连续的一段··)

  因此我们先随意找一个B为中心··然后计算答案··之后我们一次将序列最左端的字符移到最右端(比如序列BBRRR移动后就变成BRRRB)然后考虑答案的变化即可···具体实现参见代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=2e6+5;
int T,n,num[N];
char s[N];
int main()
{//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);scanf("%d",&T);while(T--){long long ans=0,cnt=0;int tot1=0,tot2=0,totl=0,totr=0,mid;scanf("%s",s+1);n=strlen(s+1);for(int i=1;i<=n;i++){if(s[i]=='B')  num[i]=num[i+n]=1,tot1++;else num[i]=num[i+n]=2,tot2++;}int half=(tot1+1)/2;int temp=0;for(int i=1;i<=n;i++){if(num[i]==1)  {temp++;if(temp==half)  mid=i;}else{if(temp<half){totl++;cnt+=temp;}else {totr++;cnt+=(tot1-temp);}}}ans=cnt;for(int i=1;i<n;i++){if(num[i]==1){ int tot=0,j;for(j=mid+1;num[j]!=1;j++)  tot++;    cnt-=totl;cnt+=(totr-tot);mid=j;totl+=tot;totr-=tot;if(tot1%2==0) cnt-=tot;ans=min(ans,cnt);}else {totl-=1;totr+=1;  }}cout<<ans<<endl;}return 0;
}

 

2.dp+决策单调性/斜率优化

  dp方程肯定很好想··第一要明确取的方案··我们肯定是以某一层的宽度为矩形的一边··然后往下取到某一层为一个矩形·矩形的高就是两层高的差··

  然后设f[j][i]为第j块矩形以第i层为一边的最大面积··转移方程即为:

  f[j][i]=max(f[j][i],f[k][i-1]+(long long)len[j]*(j-k));

  其中k为我们往下枚举的层数··len为j层的宽度··

  然后通过打表(dalao也可以分析)得出该方程满足决策单调性且使用于斜率优化····这里两种方法都能过

  决策单调性:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
inline int R()
{char c;int f=0;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c<='9'&&c>='0';c=getchar())  f=(f<<3)+(f<<1)+c-'0';return f;
}
const int N=20005;
const int M=105;
struct node
{int l,r,pos;
}Que[N];
int n,K;
long long len[N],f[N][M];
inline long long calc(int i,int j,int now)
{return f[j][now-1]+(long long)len[i]*(i-j);
}
inline int find(node a,int b,int now)
{int le=a.l,ri=a.r,ans=a.r+1;while(le<=ri){int mid=(le+ri)/2;if(calc(mid,b,now)>calc(mid,a.pos,now))  ri=mid-1,ans=mid;else le=mid+1;}return ans;
}
inline void dp(int now)
{int Head=1,Tail=0;node tmp;tmp.l=now;tmp.r=n;tmp.pos=now-1;Que[++Tail]=tmp;for(int i=now;i<=n;i++){while(Que[Head].r<i)  Head++;f[i][now]=calc(i,Que[Head].pos,now);if(calc(n,i,now)>calc(n,Que[Tail].pos,now)){while(Head<=Tail&&calc(Que[Tail].l,i,now)>calc(Que[Tail].l,Que[Tail].pos,now))  Tail--;if(Head<=Tail){int t=find(Que[Tail],i,now);Que[Tail].r=t-1;node tmp;tmp.l=t,tmp.r=n,tmp.pos=i;Que[++Tail]=tmp;}else{node tmp;tmp.l=i+1;tmp.r=n;tmp.pos=i;Que[++Tail]=tmp;}}}
}
int main()
{//freopen("pyramid.out","w",stdout);n=R(),K=R();int x,y;if(n<=1000){  for(int i=1;i<=n;i++){x=R(),y=R();len[i]=y-x+1;f[i][1]=(long long)len[i]*i;}for(int i=2;i<=K;i++)for(int j=i;j<=n;j++)for(int k=i-1;k<j;k++)  f[j][i]=max(f[j][i],f[k][i-1]+(long long)len[j]*(j-k));long long ans=0;for(int i=K;i<=n;i++)  ans=max(f[i][K],ans);cout<<ans<<"\n";}else{for(int i=1;i<=n;i++){x=R(),y=R();len[i]=y-x+1;f[i][1]=(long long)len[i]*i;}for(int i=2;i<=K;i++)dp(i);long long ans=0;for(int i=K;i<=n;i++)  ans=max(f[i][K],ans);   cout<<ans<<"\n";}return 0;
}

 

3.质因数分解+容斥原理

  这道题和之前跳蚤的那道题是一模一样的··这里就并不多说了··

  唯一注意的是我发现了自己快速幂的一个漏洞··求a^b之前要将a取模···之前一直没有注意到···还有就是注意最后答案为负的问题

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
vector<long long>zhiyinzi;
const long long mod=1e9+7;
long long n,m,ans=0;
inline long long ksm(long long a,long long b)
{long long ans=1;a%=mod;  while(b){if(b%2==1)  ans=(ans*a)%mod;b/=2;a=(a*a)%mod;}return ans;
}
inline void dfs(int u,long long tot,long long f)
{if(u==zhiyinzi.size()){long long temp=m/tot;ans=(ans+f*ksm(temp,n))%mod;ans=(ans%mod+mod)%mod;return;}dfs(u+1,tot,f);dfs(u+1,tot*zhiyinzi[u],-f);
}
int main()
{scanf("%I64d%I64d",&n,&m);long long temp=m;for(long long i=2;i*i<=m;i++){if(i>temp)  break;  if(temp%i==0){ while(temp%i==0)  temp/=i;zhiyinzi.push_back(i);}}if(temp!=1)  zhiyinzi.push_back(temp);dfs(0,1,1);ans=(ans%mod+mod)%mod;cout<<ans<<endl;return 0;
}

 

 

  

转载于:https://www.cnblogs.com/AseanA/p/7745338.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390977.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

虚幻引擎 js开发游戏_通过编码3游戏学习虚幻引擎4-5小时免费游戏开发视频课程

虚幻引擎 js开发游戏One of the most widely used game engines is Unreal Engine by Epic Games. On the freeCodeCamp.org YouTube channel, weve published a comprehensive course on how to use Unreal Engine with C to develop games.Epic Games的虚幻引擎是使用最广泛的…

建造者模式什么时候使用?

问题&#xff1a;建造者模式什么时候使用&#xff1f; 建造者模式在现实世界里面的使用例子是什么&#xff1f;它有啥用呢&#xff1f;为啥不直接用工厂模式 回答一 下面是使用这个模式的一些理由和Java的样例代码&#xff0c;但是它是由设计模式的4个人讨论出来的建造者模式…

TP5_学习

2017.10.27&#xff1a;1.index入口跑到public下面去了 2.不能使用 define(BIND_MODULE,Admin);自动生成模块了&#xff0c;网上查了下&#xff1a; \think\Build::module(Admin);//亲测,可用 2017.10.28:1.一直不知道怎么做查询显示和全部显示&#xff0c;原来如此简单&#x…

sql sum语句_SQL Sum语句示例说明

sql sum语句SQL中的Sum语句是什么&#xff1f; (What is the Sum statement in SQL?) This is one of the aggregate functions (as is count, average, max, min, etc.). They are used in a GROUP BY clause as it aggregates data presented by the SELECT FROM WHERE port…

10款中小企业必备的开源免费安全工具

10款中小企业必备的开源免费安全工具 secist2017-05-188共527453人围观 &#xff0c;发现 7 个不明物体企业安全工具很多企业特别是一些中小型企业在日常生产中&#xff0c;时常会因为时间、预算、人员配比等问题&#xff0c;而大大减少或降低在安全方面的投入。这时候&#xf…

为什么Java里面没有 SortedList

问题&#xff1a;为什么Java里面没有 SortedList Java 里面有SortedSet和SortedMap接口&#xff0c;它们都属于Java的集合框架和提供对元素进行排序的方法 然鹅&#xff0c;在我的认知里Java就没有SortedList这个东西。你只能使用java.util.Collections.sort()去排序一个list…

图片主成分分析后的可视化_主成分分析-可视化

图片主成分分析后的可视化If you have ever taken an online course on Machine Learning, you must have come across Principal Component Analysis for dimensionality reduction, or in simple terms, for compression of data. Guess what, I had taken such courses too …

回溯算法和递归算法_回溯算法:递归和搜索示例说明

回溯算法和递归算法Examples where backtracking can be used to solve puzzles or problems include:回溯可用于解决难题或问题的示例包括&#xff1a; Puzzles such as eight queens puzzle, crosswords, verbal arithmetic, Sudoku [nb 1], and Peg Solitaire. 诸如八个皇后…

C#中的equals()和==

using System;namespace EqualsTest {class EqualsTest{static void Main(string[] args){//值类型int x 1;int y 1;Console.WriteLine(x y);//TrueConsole.WriteLine(x.Equals(y));//True //引用类型A a new A();B b new B();//Console.WriteLine(ab);//报错…

JPA JoinColumn vs mappedBy

问题&#xff1a;JPA JoinColumn vs mappedBy 两者的区别是什么呢 Entity public class Company {OneToMany(cascade CascadeType.ALL , fetch FetchType.LAZY)JoinColumn(name "companyIdRef", referencedColumnName "companyId")private List<B…

TP引用样式表和js文件及验证码

TP引用样式表和js文件及验证码 引入样式表和js文件 <script src"__PUBLIC__/bootstrap/js/jquery-1.11.2.min.js"></script> <script src"__PUBLIC__/bootstrap/js/bootstrap.min.js"></script> <link href"__PUBLIC__/bo…

pytorch深度学习_深度学习和PyTorch的推荐系统实施

pytorch深度学习The recommendation is a simple algorithm that works on the principle of data filtering. The algorithm finds a pattern between two users and recommends or provides additional relevant information to a user in choosing a product or services.该…

什么是JavaScript中的回调函数?

This article gives a brief introduction to the concept and usage of callback functions in the JavaScript programming language.本文简要介绍了JavaScript编程语言中的回调函数的概念和用法。 函数就是对象 (Functions are Objects) The first thing we need to know i…

Java 集合-集合介绍

2017-10-30 00:01:09 一、Java集合的类关系图 二、集合类的概述 集合类出现的原因&#xff1a;面向对象语言对事物的体现都是以对象的形式&#xff0c;所以为了方便对多个对象的操作&#xff0c;Java就提供了集合类。数组和集合类同是容器&#xff0c;有什么不同&#xff1a;数…

为什么Java不允许super.super.method();

问题&#xff1a;为什么Java不允许super.super.method(); 我想出了这个问题&#xff0c;认为这个是很好解决的&#xff08;也不是没有它就不行的&#xff09;如果可以像下面那样写的话&#xff1a; Override public String toString() {return super.super.toString(); }我不…

Exchange 2016部署实施案例篇-04.Ex基础配置篇(下)

上二篇我们对全新部署完成的Exchange Server做了基础的一些配置&#xff0c;今天继续基础配置这个话题。 DAG配置 先决条件 首先在配置DGA之前我们需要确保DAG成员服务器上磁盘的盘符都是一样的&#xff0c;大小建议最好也相同。 其次我们需要确保有一块网卡用于数据复制使用&…

数据库课程设计结论_结论:

数据库课程设计结论In this article, we will learn about different types[Z Test and t Test] of commonly used Hypothesis Testing.在本文中&#xff0c;我们将学习常用假设检验的不同类型[ Z检验和t检验 ]。 假设是什么&#xff1f; (What is Hypothesis?) This is a St…

JavaScript数据类型:Typeof解释

typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well.typeof是一个JavaScript关键字&#xff0c;当您调用它时将…

asp.net读取用户控件,自定义加载用户控件

1、自定义加载用户控件 ceshi.aspx页面 <html><body> <div id"divControls" runat"server"></div> </body></html> ceshi.aspx.cs页面 System.Web.UI.UserControl newUC (System.Web.UI.UserControl)Page.LoadContro…

配置Java_Home,临时环境变量信息

一、内容回顾 上一篇博客《Java运行环境的搭建---Windows系统》 我们说到了配置path环境变量的目的在于控制台可以在任意路径下都可以找到java的开发工具。 二、配置其他环境变量 1. 原因 为了获取更大的用户群体&#xff0c;所以使用java语言开发系统需要兼容不同版本的jdk&a…