本周心得:差不多已经习惯了熬夜,从原来10点半准时睡觉到10点半准时打开CF……
就这周的三场比赛而言感觉挺稳定的,第一场Div2的半个小时多一点做出前两到,被C题卡住,至于第二场Unrated的那场最后一个题只能过样例;至于昨晚的一场,emmmm精神状态不太好,本来不想打的,但从来没参加过Div4的我还是想去试一下(
其实是上星期掉分太猛了,这星期前面两场又不记分),总的来说题目少了点拐弯抹角,考察思维和动手能力(说白了就是模拟,仅仅对我这样的而言找不出规律只能模拟),这场进去的晚了一点,不过也没有很晚,榜首都A完A题的样子,然后我就被A题卡住了,最后无奈只能暴力,差不多还是半个小时左右做出了前两个,C题用数学,感觉很简单就放了一下,然后D题又是暴力,不过码字真的太慢了,剩下的时间都是D题的,最后10分钟还剩才写完,可最后还是只过了样例,但是脑子真的很混乱了当时,就差一点点,愣是没找出错误来,最后结果以做出两道收尾。
这周就总结一下这周所学的单调栈和数论吧
(1)当新元素在单调性上优于栈顶时(单增栈新元素比栈顶大,单减栈新元素比栈顶小),压栈,栈深+1;
(2)当新元素在单调性与栈顶相同(新元素于栈顶相同)或劣于栈顶时(单增栈新元素比栈顶小,单减栈新元素比栈顶大),弹栈,栈深-1;// 如果题目要求严格单调的话
比如:有一组数10,3,7,4,12,22。从左到右依次入栈,则如果栈为空或入栈元素值小于栈顶元素值,则入栈;
否则,如果入栈则会破坏栈的单调性,则需要把比入栈元素小的元素全部出栈。单调递减的栈反之。
-
10入栈时,栈为空,直接入栈,栈内元素为10。
-
3入栈时,栈顶元素10比3大,则入栈,栈内元素从下往上依次为10,3。
-
7入栈时,栈顶元素3比7小,则栈顶元素出栈,此时栈顶元素为10,比7大,比较完成,最后7入栈,栈内元素从下往上依次为10,7。
-
4入栈时,栈顶元素7比4大,则入栈,栈内元素从下往上依次为10,7,4。
-
12入栈时,栈内元素都比12小,所以依次弹出:栈顶元素4比12小,4出栈,此时栈顶元素为7,仍比12小,栈顶元素7继续出栈,此时栈顶元素为9,仍比12小,出栈,9此时栈为空,12入栈,栈内元素只有12。
-
2入栈时,因为比100小,所以什么事情都不会发生
转载至:https://blog.csdn.net/lucky52529/article/details/89155694
下面看一个例题:
Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads.
Each cow i has a specified height hi (1 ≤ hi ≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.
Consider this example:
=
= =
= - = Cows facing right -->
= = =
= - = = =
= = = = = =
1 2 3 4 5 6
Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow's hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow's hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!
Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.
Input
Line 1: The number of cows, N.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.
Output
Line 1: A single integer that is the sum of c1 through cN.
Sample Input
6
10
3
7
4
12
2
Sample Output
5
题目大意 :依次给出n个数,找出从这个数开始到后面第一个大于他的数之间共有几个数x,将每一个数对应的x相加
思路:看到这个题的第一眼,就可以看出要构成单调递减序列,栈内的某一个数每多保留一会,那么就证明当前的数比栈内的数要小
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <vector> #include <map> #include <stack> #include <queue> #define rush() int T;cin>>T;while(T--) #define go(a) while(cin>>a) #define ms(a,b) memset(a,b,sizeof a) #define E 1e-8 using namespace std; typedef __int64 ll; const int idata=1e3+5; int n,m,t,_; int i,j,k; int cnt,num,ans; int minn,maxx; stack<int>stk; priority_queue<ll,vector<ll>,less<ll> >q; map<ll,int>mp; vector<int>v; int main() { cin.tie(0); iostream::sync_with_stdio(false); while(cin>>n) { ll num=0; for(i=1;i<=n;i++){ cin>>_; while(!stk.empty()&&_>=stk.top()){ stk.pop(); } num+=stk.size(); stk.push(_); } cout<<num<<endl; } return 0; }
第二部分:数论
刚刚接触数论 ,说实话,我对数学还是挺感兴趣的(虽然后来才发现我喜欢的是做数学题,而不是真正喜欢数学),这次就简单介绍一下素数筛,毕竟有些思路还是不大清楚
埃氏筛
这种筛法应该是是效率相对比较高(下文会解释),而且很好理解的筛法。
试除法是用原数去尝试诸多因子,那对于求一个范围(比如1~N)的所有素数,显然上述方法是不可取的。
那我们反向思考,尝试枚举因子去排除素数(即标记合数)。
枚举2~N,并且用2~N的倍数去标记合数,比如枚举到i,那2*i,3*i,4*i······,全部都标记为合数,直到k*i>N。(这样将2,3,5的倍数去掉后,剩下的合数就很少了,所以效率相对较高)
扫到数x时,看它是否有合数标记,如果没有,则说明2~x-1的数中,都没有它的因数,显然x-1>√x(x>2),所以x为质数。
转载至:https://www.cnblogs.com/NSD-email0820/p/9490828.html
void isPrime(int n)
{
memset(v,0,sizeof(v));//假设全是素数,无合数
for(int i=2;i<=n;i++)
{
if(!v[i])
{
prime[++cnt]=i;
for(int j=i*i;j<=n;j+=i)
v[j]=1;
}
}
}
为什么是从i*i开始筛呢?
因为当扫i的时候 2~i-1 的倍数已经全部被筛了,没必要再筛i的2~i-1倍的数,可以直接从i*i开始筛了。