- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试解决这个 SPOJ 问题 http://www.spoj.com/problems/BAISED/ .
我的方法
for all elements in the preferred_position array
if(position>0&&position<=n)
if(position is unoccupied)
allocate position for user
else
reach the first free position in the array
for all elements whose preferred position is already filled
search both directions left,right to find nearest free_position
我尝试了很多测试用例并得到了正确的结果,但我不知道我在哪里失败并得到了错误的答案。我根据贪婪标签选择了这个问题,我真的不知道在哪里应用贪婪技术。任何人都可以提出一些建议吗?
最佳答案
这是我接受的解决方案!!
我发送了几次,直到意识到数字可能非常大,所以我将所有内容都更改为 long long
简单地对数字进行排序,然后找出它们位置之间的差异。
我是如何找到这个解决方案的:
1) you should place all teams in positions from 0 to n - 1
2) lets place as many teams as possible in their preferred places
3) rest unplaced teams have the same preferred positions as placed teams or preferred positions are large then n - 1 (or n - if enumerated from 1)
4) As we remember that we need to fell all positions from 0 to n - 1, so it is obvious that if we sort the unplaced teams we could minimize the answer
为什么我跳过第二步(你可以尝试证明),让我们看一下示例测试:
1
3
first team 2
second team 3
third team 4
4 2 3 -> |1 - 4| + |2 - 2| + |3 - 3| = 3
2 3 4 -> |1 - 2| + |2 - 3| + |3 - 4| = 3
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long t;
cin >> t;
vector <long long> a;
for (long long i = 0; i < t; i++) {
long long n;
cin >> n;
a.clear();
for (long long j = 0; j < n; j++) {
string s;
cin >> s;
long long p ;
cin >> p;
p--;
a.push_back(p);
}
sort(a.begin(), a.end());
long long ans = 0;
for (long long i = 0; i < a.size(); i++) {
ans += abs(a[i] - i);
}
cout << ans << '\n';
}
}
关于algorithm - 如何解决 SPOJ 上的 BAISED?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345372/
以下是 SPOJ GCD2 的代码.它在我的机器和 Ideone 上运行良好,但在 SPOJ 上出现运行时错误 (SIGFPE)。我已经检查了 spojtoolkit.com 上也提供的所有测试用例。
如果正整数在从左到右和从右到左读取时在十进制系统中的表示相同,则称为回文。给定一个不超过1000000位的正整数K,写出大于K的最小回文的值输出。显示的数字始终不带前导零。 输入 第一行包含整数 t,
我一直在努力解决这个问题 SPOJ www.spoj.com/problems/PRHYME/?几天了,但没有成功。这是问题的简要说明: Given is a wordlist L, and a
我是编码初学者。我在向 spoj 提交质数生成代码时收到 NZEC 错误。但代码在我的桌面上运行得很好。请帮助我。这就是我编写的代码。 import java.util.*; import java.
#include #include #include main() { int n, m, i, j, k; char a[100], b[100]; scanf("%d
我遇到了一个问题 SPOJ . 我检查了所有通过所有测试用例,但我仍然在 spoj 上得到“WA”。 我知道它可以使用动态编程来解决,但我正在练习内存。 这是我的代码: #include #inclu
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
有关确切的问题,请参阅此 link 这里我定义了三个函数,将它们相互调用。函数调用尚未完成 #include int primegen(int x1,int x2); int isprime(int
Peter 想生成一些 prime numbers对于他的密码系统。帮助他!您的任务是生成两个给定数字之间的所有素数! 输入 输入以单行中测试用例的数量t开始(t int main() {
http://www.spoj.com/problems/FCTRL2/ 我的代码在 Spoj 中显示编译错误,尽管在我的编译器中运行准确。 **IDE - 代码块 ** int cal(int );
我正在尝试将我的代码提交给 SPOJ 上的“小阶乘”问题。它在我的 IDE 上成功运行,但在 SPOJ 上显示运行时错误 (NZEC)。请提出解决方案。 import java.util.Scanne
我正在尝试解决 SPOJ 中的下一个回文问题。我在下面的 Java 代码中收到超出时间限制的错误。 “如果一个正整数在十进制中从左到右和从右到左读的表示相同,则称为回文。对于给定不超过 1000000
我在 spoj 上解决了一个名为 Ambiguous Permutations(http://www.spoj.com/problems/PERMUT2/)的简单问题,当我测试小输入时它工作正常,但在
我正在尝试关于 SPOJ 的问题,其中我们必须简单地找到 Longest Increasing Sub-sequence 的长度给定数组 A. 我使用动态规划 O(n^2) 算法解决了这个问题,解决方
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我刚开始接触竞争性编程。我有点坚持这个素数。 SPOJ 上的生成器问题。代码在 GeeksforGeeks IDE 上运行良好,但在 SPOJ 上它会出现运行时错误。问题是这样的: Peter 想为他
好吧,这让我发疯了。我在 spoj 上解决了一个名为 MIXTURES ( http://www.spoj.com/problems/MIXTURES/) 的问题。我不知道为什么我总是遇到段错误。该问
我已经为下一个回文问题编写了一个蛮力解决方案,并希望获得 Time Limit Exceeded 。但是当我测试了一些测试用例时它工作正常但是当我在 spoj 中提交代码时我得到了错误的答案。这是我的
我正在尝试解决 NGON问题。我在这里使用自下而上的动态编程。递归函数为: f(a,b) = f(a-1,b) + f(a-1,b-1)*ai +f(a-1,b-2)*ai*(ai-1)/2, a>0
我正在解决这个 spoj 问题。 http://www.spoj.com/problems/MAXSUB/我所有的测试用例都正常工作,但我在 spoj 中得到了错误的答案。我试图改变我的代码很多但仍然
我是一名优秀的程序员,十分优秀!