Pairwise Sum and Divide
有這樣一段程式,fun會對整數陣列A進行求值,其中Floor表示向下取整:
fun(A)
sum = 0
for i = 1 to A.length
for j = i+1 to A.length
sum = sum + Floor((A[i]+A[j])/(A[i]*A[j]))
return sum
給出陣列A,由你來計算fun(A)的結果。例如:A = {1, 4, 1},fun(A) = [5/4] + [2/1] + [5/4] = 1 + 2 + 1 = 4。
Input
第1行:1個數N,表示陣列A的長度(1 <= N <= 100000)。 第2 - N + 1行:每行1個數A[i](1 <= A[i] <= 10^9)。
Output
輸出fun(A)的計算結果。
Input示例
3 1 4 1
Output示例
4
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int input[100000];
int fun(int n)
{
int oneNum[n];
int twoNum[n];
memset(oneNum, 0, sizeof(oneNum));
memset(twoNum, 0, sizeof(twoNum));
int sum1 = 0;
int sum2 = 0;
for (int i = n-1; i >= 0; i--)
{
if (input[i] == 1)
{
sum1++;
}
else if (input[i] == 2)
{
sum2++;
}
oneNum[i] = sum1;
twoNum[i] = sum2;
}
int result = 0;
for (int i = 0; i < n-1; i++)
{
if (input[i] == 1)
{
result += 2*oneNum[i+1];
result += n - 1 - i - oneNum[i+1];
}
else if (input[i] == 2)
{
result += oneNum[i+1] + twoNum[i+1];
}
else
{
result += oneNum[i+1];
}
}
return result;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> input[i];
}
cout << fun(n) << endl;
return 0;
}
相關文章
- Divide Two IntegersIDE
- D. Divide and EqualizeIDE
- 學習排序演算法(二):Pairwise方法之RankNet排序演算法AI
- Leetcode Divide Two IntegersLeetCodeIDE
- GCD SUMGC
- 秒殺 2Sum 3Sum 4Sum 演算法題演算法
- BigDecimal的divide方法報錯DecimalIDE
- Leetcode 29 Divide Two IntegersLeetCodeIDE
- Leetcode-Divide Two IntegersLeetCodeIDE
- Divide Two Integers leetcode javaIDELeetCodeJava
- [ABC234G] Divide a SequenceIDE
- leetcode15&16_3Sum&4SumLeetCode
- leetcode-29. Divide Two IntegersLeetCodeIDE
- [LeetCode] 29. Divide Two IntegersLeetCodeIDE
- leetcode29_Divide Two IntegersLeetCodeIDE
- SQL groupby sum 用法SQL
- Split Array Largest Sum
- oracle 字串 聚合 sumOracle字串
- Missing Subsequence Sum
- B - Minimum Sum
- Range Minimum Sum
- LeetCode T29 Divide Two IntegersLeetCodeIDE
- [LeetCode] Divide Two Integers 兩數相除LeetCodeIDE
- [LeetCode] 2491. Divide Players Into Teams of Equal SkillLeetCodeIDE
- Codeforces 1863F Divide, XOR, and ConquerIDE
- Leetcode Path SumLeetCode
- leetcode Sum系列LeetCode
- Leetcode Two SumLeetCode
- Algorithm for Maximum Subsequence Sum zGo
- 7.22 APPROX_SUMAPP
- Linux基礎命令—sumLinux
- Leetcode 1 two sumLeetCode
- Linq sum()時遇到NULLNull
- Leetcode 3SumLeetCode
- Leetcode 4SumLeetCode
- Leetcode Path Sum IILeetCode
- 演算法4Sum演算法
- 演算法3Sum演算法