amsen's blog

By amsen, history, 7 months ago, In English

Letter most:

For each lowercase letter count number of its occurrences in $$$s$$$, maximum of this value for all letters is the answer. All can be done in $$$O(n)$$$.

code

Array modification:

Assume  will be the maximum. it can be proven that all operation should be done in direction of $$$x$$$ ($$$j=i+1$$$ for all operation on its left and $$$j=i-1$$$ for all operations on its right).

Now consider $$$pref_x$$$ is maximum value of $$$a_x$$$ if all operations be done in direction of right for all $$$i=1, 2, ..., x$$$.

And consider $$$suf_x$$$ is maximum value of $$$a_x$$$ if all operations be done in direction of left for all $$$i=x, x+1, ..., n$$$.

It can bee seen that:

  • $$$pref_x = \frac{perf_{x-1}}{2} + a_x$$$.

  • $$$suf_x = \frac{suf_{x+1}}{2} + a_x$$$.

And so the answer is $$$max_{i=1}^{n}(pref_i + suf_i - a_i)$$$.

All can be done in $$$O(n)$$$.

code

Plan for nothing:

Consider an array $$$c_1, c_2, .., c_n$$$, for all intervals like $$$[l, r]$$$ do the following operation:

  • $$$c_l = c_l + 1$$$.

  • if $$$r < n$$$, $$$c_{r+1} = c_{r+1} - 1$$$.

Then consider $$$pref_i = c_1 + c_2 + ... c_i$$$. a day is good for date if and only if $$$pref_i = 0$$$.

So the answer will be minimum $$$i$$$ that $$$pref_i = 0$$$ and if it doesn't exist answer is $$$-1$$$.

code

Lonely M's array:

First reverse $$$a_1, a_2, .., a_n$$$.

Consider two following arrays:

  • $$$less_i, 1 \leq i \leq 3\times 10^5$$$: maximum length of all subsequence which ends with $$$... \leq i$$$.

  • $$$greater_i, 1 \leq i \leq 3\times 10^5$$$: maximum length of all subsequence which ends with $$$... \geq i$$$.

  • initially both are filled with zero.

Start sweep line form $$$1$$$ to $$$n$$$:

  • for each $$$a_i$$$ maximum length subsequence that ends with $$$... \leq a_i$$$ is $$$max_{j=1}^{a_i}(greater_j) + 1$$$ lets call it $$$X$$$.

  • for each $$$a_i$$$ maximum length subsequence that ends with $$$... \geq a_i$$$ is $$$max_{j=1}^{a_i}(less_j) + 1$$$ lets call it $$$Y$$$.

  • after calculating $$$X$$$ and $$$Y$$$, $$$less_{a_i}$$$ should be updated with $$$X$$$ and also greater_{a_i} should be updated with $$$Y$$$. The answer is $$$max_{i=1}^{3 \times 10^5}(less_i)$$$ because the subsequence should finish with $$$\leq$$$.

There are some RMQ(range maximum queries) and some single elements updates that all can be done using segment trees or fenwick trees (because queries are all either a prefix or suffix) in $$$log(3 \times 10^5)$$$.

So all can be done in $$$n \times log(3 \times 10^5)$$$.

code

Two papers I:

For an edge $$$E$$$, consider number of matchings that includes $$$E$$$. if this number is even $$$E$$$ can be ignored. So the answer is xor of all $$$E$$$'s weight which belongs to odd number of matching.

Consider the tree is rooted from vertex $$$1$$$ and: 

  • $$$dpDown_{v, 0}$$$: parity of number of matchings for subtree of $$$v$$$ which using $$$v$$$ is forbidden for matchings.

  • $$$dpDown_{v, 1}$$$: parity of number of matchings for subtree of $$$v$$$ which using $$$v$$$ is allowed for matchings.

Both dps can be calculated easily by a dfs from root and updating parent's dps from childs.

For an edge from $$$v$$$ to $$$par_v$$$(parent of $$$v$$$), parity of number of matching which include this edge is:

$$$dpDown_{v, 0} \times \prod_{u\ is\ par_v's\ child\ except\ v} dpDown_{u, 1} \times X$$$, that $$$X$$$ is parity of number of matchings for all vertices else subtree of $$$par_v$$$. $$$X$$$ can be calculated and passed through dfs.

So for all of edges can seen that are they in odd number of edges or even by another dfs and passing and updating $$$X$$$ through dfs arguments.

All can be done in $$$O(n)$$$.

code

Expectation:

Consider two dps:

  • $$$dpCnt_{from, to, len}$$$: number of grids with two rows and $$$2^{len}$$$ columns that are colored in black and white and its first column is $$$from$$$ and its last column is $$$to$$$.

  • $$$dpSum_{from, to, len}$$$: sum of number of maximal connected components of all grids with two rows and $$$2^{len}$$$ columns that are colored in black and white and its first column is $$$from$$$ and its last column is $$$to$$$.

for each $$$len$$$ its dps can be calculated by cutting it in two halfs, fix $$$from$$$ and $$$to$$$ for both halfs and it can be seen that:

  • $$$dpCnt_{from , to, len} = \sum (dpCnt_{from, to_l, len-1} \times dpCnt_{from_r, to, len-1})$$$.

  • $$$dpSum_{from , to, len} = \sum (dpSum_{from, to_l, len-1} \times dpCnt_{from_r, to, len-1} + dpCnt_{from, to_l, len-1} \times dpSum_{from_r, to, len-1} - X \times dpCnt_{from, to_l, len-1} \times dpCnt_{from_r, to, len-1} $$$.

  • which $$$X$$$ is number of components that are unified from touching $$$to_l$$$ and $$$from_r$$$. For answer dps can be merged like described above in a way that sum their $$$2^{len}$$$s is $$$n$$$.

All can be done in $$$O(log(n))$$$.

code

Two papers II:

Consider a random spanning tree, if it has got odd number of $$$1$$$ weighted edges the answer is YES. Otherwise if there exist a cycle which contains both $$$1$$$ weighted edges and $$$0$$$ weighted edges, we can add a $$$1$$$ weighted edge to the tree and remove a $$$0$$$ weighted edge from the tree or add a $$$0$$$ weighted edge to the tree and remove a $$$1$$$ weighted edge from the tree so number of $$$1$$$ weighted edges in the tree becomes odd and answer will be YES.

And if there isn't any cycle which contains both $$$1$$$ and $$$0$$$ weighted edges the answer is NO.

For finding the cycle there should exist a biconnected component of graph which contains both $$$1$$$ and $$$0$$$ weighted edges. So by extracting biconnected components of graph and check whether do they contain both $$$1$$$ and $$$0$$$ weighted edges or not, the problem can be solved.

All can be done in $$$O(n+m)$$$.

code

Read more »

 
 
 
 
  • Vote: I like it
  • +18
  • Vote: I do not like it

By amsen, history, 8 months ago, In English

Hello everyone!

I would like to invite all of you to participate in October Circuits '20. It's a coding marathon that opens at Oct 17, 15:30 UTC and runs for 7 days, closing on Oct 24, 15:30 UTC. Please make sure to check your time zones.

The problems were prepared by me. Thanks to Arpa for coordination and MhdMohammadi for testing and haas, Haghani, ... for creating tps and MikeMirzayanov for testlib.h .

Problem statements are short but they contain easy to ignore story parts, the stories are about two character named M and Y:  M  Y

This coding marathon challenges you with 8 problems. You are expected to solve 7 algorithmic and 1 Approximate programing problem over a period of 7 days.

The timeline of the challenge is as follows:

  • Day 0: Problem 1, Problem 2, Problem 3

  • Day 1: Problem 4, Problem 5

  • Day 4: Problem 6, Problem 7

  • Day 6: Problem 8

  • Day 7: Challenge ends

New problem statements will be published every 48 hours.

Remember, this is a rated contest and there are amazing vouchers for top three coders.

For more information, visit the contest's official web page.

UPD: There are some defects in some of tests for problems Two paper I and Expectation, I fixed tests but because of incomprehensible hackerearth admins neither me nor Arpa are not allowed to modify tests so the only way is to consider score in problem Two paper I is from 64 and in problem Expectation is from 60 (they are all correct), Sorry all.

UPD2: Editorial is published, it should be published by HacherEarth weeks ago.

Read more »

 
 
 
 
  • Vote: I like it
  • +22
  • Vote: I do not like it

By amsen, history, 9 months ago, In English

Hi all!

I was trying to understand tutorial for problem C and I think I find a mistake in it.

tutorial

In the third line it says Observe that flipping the direction of an odd cycle changes the sign of the permutation that is obviously wrong. for example in n = 3 and P = 2, 3, 1 when you flip the directions of edges P become 3, 1, 2 that has the same number of inversions(=2) and it can be proven that the parity of number of inversions depends only on number of cycles in permutation's graph.

I think the solution is not the way that described in tutorial because tutorial's answer for a triangle is zero but AC solutions print 2 for triangle that is correct.

Can anyone or ko_osaga explain solution or the tutorial?

thanks.

Read more »

 
 
 
 
  • Vote: I like it
  • +34
  • Vote: I do not like it

By amsen, history, 5 years ago, In English

Does any body knows why SGU(acm.sgu.ru) is not working? it is about 2 days that i know!

Read more »

 
 
 
 
  • Vote: I like it
  • +30
  • Vote: I do not like it

By amsen, history, 5 years ago, In English

Here are iran's team for IOI 2016 (russia) sorted by rating.

1- Ali Bahjati LiTi (last year gold medal)

2- AmirMohammad Dehghan PrinceOfPersia

3- Arash Mahmoudian Reyna

4- SeyedParsa Mirtaheri SeyedParsa

thanks.

UPD: results:

1-Ali Bahjati — Gold Medal (rank 11)

2-Arash Mahmoudian — Gold Medal (rank 13)

3-AmirMohammad Dehghan — Silver Medal (rank 31)

4-SeyedParsa Mirtaheri — Silver Medal (rank 54)

Read more »

 
 
 
 
  • Vote: I like it
  • +92
  • Vote: I do not like it

By amsen, history, 5 years ago, In English

I see lots of problems about cout/cin , most of them are telling that cin/cout are slow and it's better to use scanf/printf;

but cin/cout are prettier and more easy to code for c++ coders. so i try to make an IO that it's pretty and fast.


#include <bits/stdc++.h> using namespace std; class FastIO{ public: //Output operators : inline FastIO & operator << (const int &a){ printf("%d" , a); return *this; } inline FastIO & operator << (const long long &a){ printf("%I64d" , a); return *this; } inline FastIO & operator << (const double &a){ printf("%.9f" , a); return *this; } inline FastIO & operator << (const long double &a){ printf("%.9lf" , a); return *this; } inline FastIO & operator << (const char * const&a){ printf("%s" , a); return *this; } inline FastIO & operator << (const string &a){ printf("%s" , a.c_str()); return *this; } //Input operators : inline FastIO & operator >> (int &a){ scanf("%d" , &a); return *this; } inline FastIO & operator >> (long long &a){ scanf("%I64d" , &a); return *this; } inline FastIO & operator >> (double &a){ scanf("%lf" , &a); return *this; } inline FastIO & operator >> (long double &a){ scanf("%lf" , &a); return *this; } inline FastIO & operator >> (char * const&a){ scanf("%s" , a); return *this; } }fastIO;//you can change it to cin/cout int main(){ int a;long long b;double c; fastIO >> a >> b >> c << a << " , " << b << " , " << c << "\n"; }

Read more »

 
 
 
 
  • Vote: I like it
  • +16
  • Vote: I do not like it

By amsen, history, 6 years ago, In English

How to become strong and expert in DP ?

can any body tell me some problems and some links to read ?

thanks all!

Read more »

 
 
 
 
  • Vote: I like it
  • -3
  • Vote: I do not like it