algorithm vito family - andstudy/forge GitHub Wiki
-
λΉμ°ν Solved
/* @BEGIN_OF_SOURCE_CODE */ /* @JUDGE_ID: hoimann 10041 C "median of numbers" */ /* $Id$ */ #include <stdio.h> #define MAX_N 500 void main(void) { int a[MAX_N]; int num_cases, n; int left, right, i, j, pivot, temp, sum_dist; scanf("%d", &num_cases); while (num_cases-- > 0) { scanf("%d", &n); for (i=0 ; i<n ; i++) scanf("%d", &(a[i])); left = 0; right = n - 1; do { pivot = a[left]; i = left; j = right; while (i <= j) { while (i <= right && a[i] <= pivot) i++; while (j > left && a[j] >= pivot) j--; if (i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } a[left] = a[j]; a[j] = pivot; if (j < n/2) left = j + 1; else right = j - 1; } while (j != n/2); sum_dist = 0; for (i=0 ; i<j ; i++) sum_dist += (pivot - a[i]); for (i=j+1 ; i<n ; i++) sum_dist += (a[i] - pivot); printf("%d\n", sum_dist); } } /* @END_OF_SOURCE_CODE */
-
vito κ° μνλ μ§μ μ μ°Ύμ§ μκ³ λ¨μν κ° μΉμ²μ§λ€μ 거리λ§μ κ³μ°ν΄μ ν©νμλ€μ. --
-
λ€μ μμ νμ΅λλ€.
-
VitoMain
import java.io.*; import java.util.ArrayList; import java.util.Collections; class VitoMain { public static void main(String args[]) { VitoMain vito = new VitoMain(); vito.run(); } public void run() { int testCase = Integer.parseInt(readLine()); String printResult = ""; if( testCase <= 0 || testCase >= 500) return; while(testCase != 0) { printResult += "" + sumDistance(readLine()) + "\n"; testCase--; } System.out.println(printResult); } /* sort ν μ€κ°μ§μ μ vito point λ‘ */ public int sumDistance(String in) { ArrayList<Integer> famList = new ArrayList<Integer>(); String[] data = in.split(" "); for(int i=1; i< data.length ; i++) { famList.add(Integer.parseInt(data[i])); } Collections.sort(famList); int result=0; for(int i=0; i< famList.size(); i++) { result += Math.abs(famList.get(Integer.parseInt(data[0])/2) - famList.get(i)); } return result; } public String readLine() { String data = null; try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); data = is.readLine(); } catch(IOException e) { System.out.println("IOException " +e); } return data; } } -
Junit
import junit.framework.TestCase; public class VitoMainTest extends TestCase { VitoMain vito = new VitoMain(); public void testAddVitoFam() { assertEquals("1",2,vito.sumDistance("2 2 4")); assertEquals("1",4,vito.sumDistance("3 2 4 6")); assertEquals("1",3,vito.sumDistance("2 5 2")); assertEquals("1",5,vito.sumDistance("4 4 2 3 6")); assertEquals("1",100,vito.sumDistance("5 2 3 4 5 100")); } }
- κ²°κ³Όλ wrong answer...
- μ¬λ¬ μΌμ΄μ€ μ λ ₯μ ν μΌμ΄μ€ μ λ ₯ ν λ°λ‘ λ΅μ΄ λμμΌ ν©λλ€.
- νμ¬ μνλ λͺ¨λ μΌμ΄μ€ μ λ ₯ ν λͺ¨λ λ΅μ΄ νλ²μ μΆλ ₯ λλ€μ.
- μ λ ₯κ°μ 50μ΄μμ μ«μκ° λ€μ΄κ°λ©΄ RTEκ° λ°μνλ€μ. λ¬Έμ μ λͺ μλ Inputμ λ²μλ κ³ λ €νμ μΌ ν©λλ€.
- μ λ ¬μνκ³ λ£¨νλ‘ λλ €μ νμ΅λλ€ O(N^2)
- μμ λ¬Έμ λ₯Ό λͺ¨λ κ³ μ³€μ΅λλ€. μμ μ±λ₯μ μλμ€λκ΅°μ γ
γ
γ
-
μΉμ²μ΄ μ΄μ§ μλ κ³³μ κ²μ¬νμ§ μμΌλ μ±λ₯μ΄ μ’μμ§κΈ°λ νλλ° μ’ μ°μ°ν©λλ€..
/* @JUDGE_ID:itmentor 110401 C "test" */
/* @BEGIN_OF_SOURCE_CODE */
#ifdef UNIT_TEST #include <UnitTest++.h> #include <TestReporterStdout.h> #endif
#include #include #include #include #include #include
using namespace std;
class Map { protected: vector _position; const int MAP_SIZE;
public: Map() : MAP_SIZE(30000) { this->clear(); }
void reside(int index) { _position[index - 1]++; } const int& operator[](int index) { return _position[index - 1]; } void clear() { _position.clear(); _position.resize(MAP_SIZE); } int length() { int beg = this->getFirstAddress() - 1; int end = this->getLastAddress() - 1; //λͺ¨λ λ²μ§μ λͺ¨λ μ¬λμ΄ μ΄κ³ μμ κ²½μ° κ±°λ¦¬μ μ΅μκ° int minDistance = (MAP_SIZE + 1) * MAP_SIZE / 2; for(int i = beg ; i <= end ; i++) { int distance = 0; if( _position[i] != 0 ) { for(int k = beg ; k <= end ; k++) { if( _position[k] > 0 ) distance += _position[k] * abs(i-k); } if( distance != 0 && minDistance > distance ) minDistance = distance; } } return minDistance; } int getFirstAddress() { for(int i = 0 ; i < MAP_SIZE ; i++) { if( _position[i] > 0 ) return i+1; } return 0; } int getLastAddress() { for(int i = MAP_SIZE - 1 ; i >= 0 ; i--) { if( _position[i] > 0 ) return i+1; } return 0; }};
int main() { #ifdef UNIT_TEST UnitTest::RunAllTests(); #endif
int count = 0; cin >> count; cin.ignore(); Map m; for(int i = 0 ; i < count ; i++) { char ln[256]; cin.getline(ln,256); stringstream sout; sout << std::string(ln); int addrCount = 0; sout >> addrCount; for(int k = 0; k < addrCount ; k++) { int addr; sout >> addr; m.reside(addr); } cout << m.length() << endl; m.clear(); } return 0;}
#ifdef UNIT_TEST TEST(basicIO) { Map m; m.reside(4); CHECK_EQUAL(m[4],1); }
TEST(getFirstAddress) { Map m; CHECK_EQUAL(m.getFirstAddress(), 0);
m.reside(30); CHECK_EQUAL(m.getFirstAddress(), 30); m.reside(50); CHECK_EQUAL(m.getLastAddress(), 50);} TEST(distance) { Map m; m.reside(2); m.reside(4); int l = m.length(); CHECK_EQUAL(l, 2); m.reside(6); l = m.length(); CHECK_EQUAL(l, 4); }
TEST(distance2) { Map m; m.reside(2); m.reside(3); int l = m.length(); CHECK_EQUAL(l, 1); }
TEST(distance3) { Map m; m.reside(13); m.reside(17); m.reside(17); m.reside(19); m.reside(22); m.reside(22); m.reside(55); m.reside(100); m.reside(101); m.reside(22); m.reside(22); m.reside(23); m.reside(23); m.reside(24); m.reside(100); m.reside(101);
int l = m.length(); CHECK_EQUAL( l, 373 );} #endif
/* @END_OF_SOURCE_CODE */
-
-
κ³μ wrong answer κ° λ¨λ€μ.
-
μ΄λ€ ν μ€νΈ μΌμ΄μ€λ₯Ό λμΉκ³ μλμ§ λͺ¨λ₯΄κ² μ΅λλ€. :(
- μ λ§ λͺ¨λ₯΄κ² λ€μ. λλ΅ λκ°.
- κ΅³μ΄ μ°Ύλλ€λ©΄ λ¬Έμ μ건μ μ΄κΈλλ μμ μ£Όμμ μ΄λ...
- μ
λ ₯κ°μ΄ μμλ₯Ό 보μ₯ν΄ μ£Όλ κ±° μλμλμ?
- μ λ ₯κ° μμ 보μ₯ν΄ μ£Όλκ² λ§μ΅λλ€. κ·Όλ° ν΄λ΅μ μμλ μ²λ¦¬νλκ΅°μ.
- κ·Έλμ κ΅³μ΄ μ°Ύλλ€λ©΄... μ΄λΌκ³ νμ΅λλ€. μ wrong answer μΈμ§ λͺ¨λ₯΄κ² λ€μ.
- 30000 μ΄μ κ°λ μμΈμΈλ° μ μ²λ¦¬νλκΉμ. μΉμ²μ μκ° 500κ° λκ² μ λ ₯λ λλ ν μ€νΈ λͺ» ν΄λ΄€μ΄μ.
-
16 13 17 17 19 22 22 55 100 101 22 22 23 23 24 100 101 μ΄ κ²½μ°μ μ ννμ§ μμ λ΅μ΄ λμ€λ€μ. μ λ΅μ 373 μ λλ€.
-
κ³Όμ° λ²κ·Έκ° μμλ€μ. ν... * μ ν¬μ’
/* @JUDGE_ID:parkpd 110401 Cpp "test" */ /* @BEGIN_OF_SOURCE_CODE */ #include <iostream> #include <vector> #include <set> #include <string> #include <strstream> #include <algorithm> #include <map> //#define _UNIT_TEST using namespace std; namespace ATUtil { bool IsInRange(int value, int from, int to) { return (from <= value) && (value <= to); } int ConvertToIndex(char c) { if ('a' <= c && c <= 'z') { return c - 'a'; } else { return -1; } } char ConvertToChar(int i) { return (char)i + 'a'; } typedef map<int, int> IntDB; typedef vector<int> Ints; }; using namespace ATUtil; #ifdef _UNIT_TEST #include "../UnitTest++/src/UnitTest++.h" int main() { UnitTest::RunAllTests(); char temp; cin >> temp; return 0; } #endif // code implement namespace ATCode { class CSolver { public: unsigned int GetDistanceSumFromPos(int i); unsigned int FindOptimalPos(); unsigned int _FindOptimalPos(int from, int to); void Insert(int num); template<typename InputIterator> void Inserts(InputIterator beg, InputIterator end); template<typename InputIterator> static unsigned int GetSolution(InputIterator beg, InputIterator end); IntDB m_FamilyStreetDB; }; unsigned int CSolver::GetDistanceSumFromPos(int i) { unsigned int sum = 0; IntDB::iterator it = m_FamilyStreetDB.begin(); for (; it != m_FamilyStreetDB.end(); ++it) { int addr = it->first; int nums = it->second; sum += (abs(i - addr) * nums); } return sum; } unsigned int CSolver::FindOptimalPos() { IntDB::iterator from = m_FamilyStreetDB.begin(); IntDB::reverse_iterator to = m_FamilyStreetDB.rbegin(); return _FindOptimalPos(from->first, to->first); } unsigned int CSolver::_FindOptimalPos(int from, int to) { int fromV = GetDistanceSumFromPos(from); int toV = GetDistanceSumFromPos(to); int mid = (to + from) / 2; if (from == mid) // κ²°μ μ μκ° { return min(fromV, toV); } if (fromV < toV) { return _FindOptimalPos(from, mid); } else if (toV < fromV) { return _FindOptimalPos(mid, to); } else { return min(_FindOptimalPos(from, mid), _FindOptimalPos(mid, to)); } } void CSolver::Insert(int num) { m_FamilyStreetDB[num]++; } template<typename InputIterator> void CSolver::Inserts(InputIterator beg, InputIterator end) { for (InputIterator it = beg; it != end; ++it) { m_FamilyStreetDB[*it]++; } } template<typename InputIterator> unsigned int CSolver::GetSolution(InputIterator beg, InputIterator end) { CSolver s; s.Inserts(beg, end); return s.FindOptimalPos(); } class CConsole { public: static void ConsoleTest(istream& input, ostream& output); }; void CConsole::ConsoleTest(istream& input, ostream& output) { //ostrstream tempOutput; int testCount = 0; input >> testCount; int relatives = 0; int streetNumber = 0; for (int i = 0; i < testCount; ++i) { input >> relatives; CSolver s; for (int j = 0; j < relatives; ++j) { input >> streetNumber; s.Insert(streetNumber); } //tempOutput << s.FindOptimalPos() << std::endl; output << s.FindOptimalPos() << std::endl; } //tempOutput << std::ends; //output << tempOutput.str(); }; } using namespace ATCode; #ifndef _UNIT_TEST int main() { CConsole::ConsoleTest(cin, cout); return 0; } #else // tests TEST(FindOptimalPos) { int inputs[] = {2}; CHECK_EQUAL(0, CSolver::GetSolution(&inputs[0], &inputs[1])); } TEST(FindOptimalPos1) { int inputs[] = {2, 4, 6}; CHECK_EQUAL(4, CSolver::GetSolution(&inputs[0], &inputs[3])); } TEST(FindOptimalPos2) { // κ°μ‘±μ΄ κ°μ μ£Όμμ λ μ΄μ μ¬λ κ²½μ°. int inputs[] = {2, 2, 10}; CHECK_EQUAL(8, CSolver::GetSolution(&inputs[0], &inputs[3])); // μμΉλ 2 } TEST(FindOptimalPos3) { int inputs[] = {2, 4}; CHECK_EQUAL(2, CSolver::GetSolution(&inputs[0], &inputs[2])); } TEST(FindOptimalPos4) { int inputs[] = {5, 2}; CHECK_EQUAL(3, CSolver::GetSolution(&inputs[0], &inputs[2])); } TEST(FindOptimalPos5) { int inputs[] = {4, 2, 3, 6}; CHECK_EQUAL(5, CSolver::GetSolution(&inputs[0], &inputs[4])); } TEST(FindOptimalPos6) { int inputs[] = {2, 3, 4, 5, 100}; CHECK_EQUAL(100, CSolver::GetSolution(&inputs[0], &inputs[5])); } TEST_FIXTURE(CSolver, GetDistanceSumFromPos) { int inputs[] = {2, 6, 8}; Inserts(&inputs[0], &inputs[3]); CHECK_EQUAL(6, FindOptimalPos()); CHECK_EQUAL(10, GetDistanceSumFromPos(2)); CHECK_EQUAL(9, GetDistanceSumFromPos(3)); CHECK_EQUAL(8, GetDistanceSumFromPos(4)); CHECK_EQUAL(7, GetDistanceSumFromPos(5)); CHECK_EQUAL(6, GetDistanceSumFromPos(6)); CHECK_EQUAL(7, GetDistanceSumFromPos(7)); CHECK_EQUAL(8, GetDistanceSumFromPos(8)); } TEST(ConsoleTest) { stringstream input; stringstream output; input << "2\n2 2 4\n3 2 4 6\n"; CConsole::ConsoleTest(input, output); CHECK_EQUAL("2\n4\n", output.str()); } #endif /* @END_OF_SOURCE_CODE */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class VitosFamily
{
public static void main(String[] args) throws IOException
{
int n = readLineInt();
// variable setting
Vito[] vitoArray = new Vito[n];
int[] distanceArray = new int[n];
// read input data
for (int i=0; i<n; i++)
{
String record = readLineString();
Vito vito = new Vito(record);
vitoArray[i] = vito;
}
// get distance
for (int i=0; i<n; i++)
{
int midAddress = vitoArray[i].getMiddleAddress();
int distance = vitoArray[i].getTotalDistanceFrom(midAddress);
distanceArray[i] = distance;
}
// print distance
for (int i=0; i<n; i++)
{
System.out.println(distanceArray[i]);
}
}
public static int readLineInt() throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
int size = Integer.parseInt(line);
return size;
}
public static String readLineString() throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
return line;
}
}
import java.util.Arrays;
import java.lang.Math;
public class Vito
{
private int[] addressArray;
private int length;
Vito(String record)
{
String[] token = record.split(" ");
length = Integer.parseInt(token[0]);
if (length != token.length-1)
{
System.out.println("Error: Invalid Record");
return;
}
addressArray = new int[length];
// read data
for (int i=0; i<length; i++)
{
addressArray[i] = Integer.parseInt(token[i+1]);
}
}
public int size()
{
return length;
}
public String toString()
{
String msg="{";
for (int i=0; i<length; i++)
{
msg += " (" + addressArray[i] + ") ";
}
msg += "}";
return msg;
}
public int getMiddleAddress()
{
Arrays.sort(addressArray);
int index = (length-1)/2;
return addressArray[index];
}
public int getTotalDistanceFrom(int add)
{
int totalDistance = 0;
for (int i=0; i<length; i++)
{
totalDistance += Math.abs(add - addressArray[i]);
}
return totalDistance;
}
}
import junit.framework.TestCase;
import kr.re.etri.algorithm.Vito;
public class VitoTest extends TestCase
{
Vito vito;
public VitoTest(String name)
{
super(name);
}
public void testSize()
{
String record = "2 3 2";
vito = new Vito(record);
assertEquals(2, vito.size());
}
public void testGetMiddleAddress()
{
String record = "2 3 2";
vito = new Vito(record);
assertEquals(2, vito.getMiddleAddress());
record = "4 2 3 4 5";
vito = new Vito(record);
assertEquals(3, vito.getMiddleAddress());
record = "3 2 3 4";
vito = new Vito(record);
assertEquals(3, vito.getMiddleAddress());
record = "3 2 4 6";
vito = new Vito(record);
assertEquals(4, vito.getMiddleAddress());
}
public void testGetTotalDistanceFrom()
{
String record = "2 2 3";
vito = new Vito(record);
assertEquals(1, vito.getTotalDistanceFrom(vito.getMiddleAddress()));
record = "2 2 4";
vito = new Vito(record);
assertEquals(2, vito.getTotalDistanceFrom(vito.getMiddleAddress()));
record = "3 2 4 6";
vito = new Vito(record);
assertEquals(4, vito.getTotalDistanceFrom(vito.getMiddleAddress()));
record = "5 2 3 4 5 100";
vito = new Vito(record);
assertEquals(100, vito.getTotalDistanceFrom(vito.getMiddleAddress()));
}
}
- μ μΆλ ₯ μ²λ¦¬ νμ§ μμ
- μ€κ°κ° ꡬν λ€ κ±°λ¦¬ ν© κ³μ°
- μνμ μ¦λͺ μ΄ νμν κΉ?
' $Id: Vito.vb 91 2008-02-19 01:16:33Z μ ν¬μ’
$
Public Class Vito
Function GetDistanceSum(ByVal middle, ByVal addresses) As Integer
Dim distanceSum As Integer
For Each address As Integer In addresses
distanceSum += Math.Abs(middle - address)
Next
Return distanceSum
End Function
Function GetMiddle(ByVal addresses As List(Of Integer)) As Integer
addresses.Sort()
Dim middleIndex As Integer = (addresses.Count / 2)
Return addresses(middleIndex)
End Function
End Class
' $Id: ModuleTest.vb 91 2008-02-19 01:16:33Z μ ν¬μ’
$
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Module ModuleTest
Private _vito As Vito = New Vito
Sub run()
DivideIngeterTest()
GetMiddleTest()
GetDistanceSumTest()
GetDistanceSumMoreTest()
End Sub
Sub GetDistanceSumMoreTest()
Dim input() As Integer = {13, 17, 17, 19, 22, 22, 55, 100, 101, 22, 22, 23, 23, 24, 100, 101}
Dim addresses As List(Of Integer) = New List(Of Integer)(input)
Dim middle As Integer = _vito.GetMiddle(addresses)
Assert.AreEqual(23, middle)
Assert.AreEqual(373, _vito.GetDistanceSum(middle, addresses))
End Sub
Sub GetDistanceSumTest()
Dim input() As Integer = {9, 1, 9, 2, 9, 3}
Dim addresses As List(Of Integer) = New List(Of Integer)(input)
Dim middle As Integer = _vito.GetMiddle(addresses)
Assert.AreEqual(21, _vito.GetDistanceSum(middle, addresses))
End Sub
Sub GetMiddleTest()
Dim input() As Integer = {9, 1, 9, 2, 9, 3}
Dim addresses As List(Of Integer) = New List(Of Integer)(input)
Assert.AreEqual(9, _vito.GetMiddle(addresses))
End Sub
Sub DivideIngeterTest()
' μ μλ₯Ό λλλ©΄ μλμΌλ‘ μ€μκ° λλ€
Assert.AreEqual(2.5, 5 / 2)
' μ μλ‘ μ μΈν΄μ λ°μΌλ©΄ μ μκ° λλ€ (μμμ μ΄ν λ²λ¦Ό)
Dim val As Integer = 5.5 / 2.2
Assert.AreEqual(2, val)
End Sub
End Module
-
ν΄κ²° λ°©λ²
- μ λ ₯ λ°μ μ§μ λ²μ§μ μ΅λμ μ΅μκ°μ ꡬν΄μμ μ€κ°κ°μΌλ‘ λΉν μ μ§μ μμΉ κ΅¬ν¨
- setμ μ΄μ©ν΄μ μ£Όμ μ€λ³΅μ μ κ±°νκ³ λΉν μ μ§μμμ κ° κ±°λ¦¬μ ν¨μ ꡬν¨
-
μμ€
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; import java.util.Set; class Main implements Runnable{ public int getVitoHousePos(int [] addrs){ int max=Integer.MIN_VALUE ; int min=Integer.MAX_VALUE; for(int curVal : addrs){ min = Math.min(min, curVal); max = Math.max(max, curVal); } int midPos = min + (max-min)/2; return midPos; } public int getRelativeDistance(int[] datas,int housePos) { Set<Integer> set = new HashSet<Integer>(); for(int curInt : datas){ set.add(curInt); } int sum=0; Iterator<Integer> i=set.iterator(); while(i.hasNext()){ int curInt = i.next(); sum += Math.abs(curInt - housePos); } return sum; } public static void main(String[] args) throws Exception, IOException { Main main = new Main(); main.run(); } public void run() { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int cases; try { cases = Integer.parseInt(reader.readLine().trim()); for(int i=0;i<cases;i++){ Scanner scanner = new Scanner(System.in); int itemSize = scanner.nextInt(); int [] datas = new int[itemSize]; for(int j=0;j<itemSize;j++){ datas[j] = scanner.nextInt(); } int housePos = getVitoHousePos(datas); int result = getRelativeDistance(datas, housePos); System.out.println(result); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } -
TestCase
import static org.junit.Assert.*; import org.junit.Test; public class MainTest extends Main{ private int [] test1 = {2,4}; private int [] test2 = {2,4,6}; private int [] test3 = {2,3,2}; @Test public void testGetVitoHouse(){ Main main = new Main(); assertEquals(3, main.getVitoHousePos(test1)); assertEquals(4, main.getVitoHousePos(test2)); assertEquals(2, main.getVitoHousePos(test3)); } public void testGetRelativeDistance(){ Main main = new Main(); assertEquals(2, main.getRelativeDistance(test1,main.getVitoHousePos(test1))); assertEquals(4, main.getRelativeDistance(test2,main.getVitoHousePos(test2))); assertEquals(1, main.getRelativeDistance(test3,main.getVitoHousePos(test3))); } }
-
ν μ€νΈ μ½λλ μ΄λ»κ² μ μ©νλμ§ μμ§ λͺ¨λ₯΄κ² μ΄μ @_@)
#include <vector> #include <algorithm> #define abs(x) ((x)>0?(x):-(x)) using namespace std; int main() { int TestCase; scanf("%d", &TestCase); while( TestCase-- ) { int family; vector<int> arrayFamily; scanf("%d", &family); int temp; for(int i = 0; i < family; i++ ) { scanf("%d", &temp); arrayFamily.push_back(temp); } sort(arrayFamily.begin(), arrayFamily.end()); temp = arrayFamily[arrayFamily.size()/2]; int result = 0; for(int i = 0; i < arrayFamily.size(); i++ ) { result += abs(temp - arrayFamily[i]); } printf("%d\n", result); } return 0; }
- Median μ΄ μ΅μ ν΄μΈκ°?
- μ λκ° ν¨μμ μ΅μκ°μ΄ Median μΈκ°?
- Median μμ΄ O(n)ν΄λ²
-
μ΄ λ¬Έμ μμ 거리λ₯Ό ꡬνλ 곡μμ΄ Dij= |Si-Sj|. μλ€.
-
μ¦ 1 3 5 7 9 κ° μμ λ, κ° μμμμ μ°¨μ΄μ ν©μ΄ μ΅μκ°μ μμμΌλ‘ λνλ΄λ©΄ λ€μκ³Ό κ°λ€.
|1-m| + |3-m| + |5-m| + |7-m| + |9-m| = μ΅μκ° -
μ¦, "μ λκ° + μ λκ° + μ λκ° + μ λκ° + μ λκ° = μ΅μκ°" κ³Ό κ°λ€.
-
μ λκ° ν©μ μ΅μ κ°μ ꡬνκΈ° μν΄μλ μμλλ‘ λμμ λ μ€κ° μμκ° 0μ΄ λλ κ°μ΄λ―λ‘,
-
μ΄ κ²½μ°μλ |5-m| μ΄ 0 μ΄λλ κ²½μ° μΌ κ²μ΄λ€.
< μΆκ° μ¦λͺ μλ > μ μμ νλ©΄, (m-1) + (m-3) + (7-m) + (9-m) = mμ μκ΄μμ΄ μμλ§μΌλ‘ λ΅μ΄ λμ΄!! μ¦, median μ μ€μ¬μΌλ‘ μ’μ°μ κ°μκ° κ°λ€λ©΄ m μ λ€ μ§μμ§λ―λ‘ μ΅μκ° μμC λλ κ²μ΄κ³ , μ’μ°μ κ°μκ° λ€λ₯΄λ€λ©΄, μμC - m μ΄ λμ μ΅μκ°μ΄ λλ κ²!
- λλμ΄ μ 체 λΉμ© O(n) μκ³ λ¦¬μ¦
-
ν€μΈλ±μ±μ μ΄μ©ν΄ μ λ ₯μ μ λ ¬νλ€ O(n)
-
1 3 5 7 9 κ° μλ€κ³ ν λ. μμμ λΆν° μ€μΊνλ©΄μ λμ ν©μ ν μ΄λΈλ‘ λ§λ€μ΄ λλλ€.
-
μ κ³Όμ μ ν΅ν΄ μ΅μκ°μ λνλ΄λ μμ΄ν μ μ°Ύμ μλ°!
-
ν΅μ¬μ μ€μΊλμ ν΅ν΄ λμ ν©μ κ΅¬ν΄ λλλ€λ κ²!
A[5] = { 1, 3, 5, 7, 9 } .... // νμ¬κΉμ§μ λμ ν© λ°°μ΄ acc[5] = { 1, 4, 9, 16, 25 } for( int i=0; i<5; i++ ) { // νμ¬ μμκ° μ΅μ ν΄λΌλ©΄! // ( νμ¬ μμ * μμ νλͺ©κ°μ - νμ¬λμ ν©) // + [ ( μ 체λμ ν© - νμ¬λμ ν© ) - λ¨μνλͺ©κ°μ * νμ¬μμ ] int left = A[i] * i - acc[i]; int right = ( acc[5] - acc[i] ) - (5-i) * A[i]; if( left + right κ° νμ¬κΉμ§ μ΅μλ? ) ..... }
-
- μ΄μ¬ν κ°κ³ μμ΅λλ€ ^^;; 20λΆ λ¦μκ² κ°μμ¬~~