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.pswp.cn/news/390977.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390977.shtml
英文地址,請注明出處:http://en.pswp.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…