Блог пользователя srinivas_8025

Автор srinivas_8025, история, 7 лет назад, По-английски

These codes have been taken from a blog thread on codeforces.

Euler Totient function

int[] phi = new int[n + 1];
for (int i = 1; i <= n; ++i) {
  phi[i] += i;
  for (int j = 2 * i; j <= n; j += i) {
    phi[j] -= phi[i];
  }
}

Floyd-Warshall

for(int k = 0; k < n; k++)
  for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++)
      d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

Iterative implementation of GCD

int gcd(int a,int b){
while (a&&b)a>b?a%=b:b%=a;
return a+b;
}

Recursive implementation of GCD

int gcd(int a, int b){
if(b==0) return a;
return gcd(b,a%b);
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

Автор srinivas_8025, история, 8 лет назад, По-английски

Editorial for Weather Problem.

This problem can be solved easily with dynamic programming.

The answer would always be the count of non-positive numbers in the segment arr[k+1,n] + count of the non-negative numbers in the segment arr[1,k] where 1<=k<n. All we have to do is to keep track of the occurrences of negative and positive integers till ith index.

Let's denote the count of negative numbers by neg[1...n] and pos[1...n] for positive numbers count. So, the count of non-negative numbers in the segment arr[1,i] = i — neg[i]. Similarly, the count of non-positive numbers in the segment arr[i+1,n]= n-i-pos[n]+pos[i]].

Atlast the answer would be min(n-neg[i]-pos[n]+pos[i]) for 1<=i<n.

You can find the code here

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится