Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.52 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <queue>
  4. #include <string>
  5. #include <vector>
  6. #include <random>
  7. #include <iomanip>
  8. using namespace std;
  9. vector<int> r,d,p,w;
  10. int n,t;
  11.  
  12. int F(vector<int> &p2)
  13. {
  14. int ans = 0;
  15. for (auto it : p2) {
  16. ans += w[it];
  17. }
  18. return ans;
  19. }
  20.  
  21. int T(vector<int> &p1, int t, int blocked) {
  22. int tm = t;
  23. for (auto it:p1) {
  24. if (it != blocked) {
  25. if (r[it]>tm)
  26. tm = r[it];
  27. tm += p[it];
  28. }
  29. }
  30. return tm;
  31. }
  32.  
  33. void ConsoleInput()
  34. {
  35. cerr << "Введите n: ";
  36. cin >> n;
  37. cerr << "Введите t: ";
  38. cin >> t;
  39. r.resize(n);
  40. cerr << "Введите r_i: ";
  41. for (int i = 0; i < n; i++)
  42. cin >> r[i];
  43. d.resize(n);
  44. cerr << "Введите d_i: ";
  45. for (int i = 0; i < n; i++)
  46. cin >> d[i];
  47. p.resize(n);
  48. cerr << "Введите p_i: ";
  49. for (int i = 0; i < n; i++)
  50. cin >> p[i];
  51. w.resize(n);
  52. cerr << "Введите w_i: ";
  53. for (int i = 0; i < n; i++)
  54. cin >> w[i];
  55. }
  56.  
  57. bool FileInput(const string &path) // считывание с файла
  58. {
  59. ifstream file_in(path);
  60. if (!(file_in >> n >> t))
  61. return false;
  62. r.resize(n);
  63. for (int i = 0; i < n; i++)
  64. if (!(file_in >> r[i]))
  65. return false;
  66. d.resize(n);
  67. for (int i = 0; i < n; i++)
  68. if (!(file_in >> d[i]))
  69. return false;
  70. p.resize(n);
  71. for (int i = 0; i < n; i++)
  72. if (!(file_in >> p[i]))
  73. return false;
  74. w.resize(n);
  75. for (int i = 0; i < n; i++)
  76. if (!(file_in >> w[i]))
  77. return false;
  78. return true;
  79. }
  80.  
  81.  
  82.  
  83. pair<vector<int>, vector<int>> FirstModification()
  84. {
  85. int tk = t;
  86. vector<int> p1, p2;
  87. queue<int> nk;
  88. for (int i = 0; i < n; i++)
  89. nk.push(i);
  90. while (!nk.empty()) {
  91. int i = nk.front();
  92. if (r[i]>tk)
  93. tk = r[i];
  94. if (tk + p[i] <= d[i])
  95. {
  96. p2.push_back(i);
  97. tk += p[i];
  98. nk.pop();
  99. } else {
  100. int ind1 = 0;
  101. int ind2 = 0;
  102. p2.push_back(i);
  103.  
  104. vector<int> cand(p2.size());
  105. for (int j = 0; j < p2.size(); j++) {
  106. cand[j] = T(p2, t, p2[j]);
  107. if (cand[ind1] > cand[j]) {
  108. ind1 = j;
  109. }
  110. if (w[p2[ind2]]> w[p2[j]]) {
  111. ind2 = j;
  112. }
  113. }
  114. int m = -1;
  115. for (int j = 0; j < p2.size(); j++) {
  116. if (cand[j]== cand[ind1] && w[p2[j]] == w[p2[ind2]]) {
  117. m = j;
  118. }
  119. }
  120. if (m == -1)
  121. {
  122. m = ind1;
  123. for (int j = 0; j < p2.size(); j++)
  124. if (cand[j]== cand[m] && w[p2[j]] < w[p2[m]]) {
  125. m = j;
  126. }
  127. }
  128. if (p2[m] == i) {
  129. p2.pop_back();
  130. p1.push_back(i);
  131. nk.pop();
  132. } else {
  133. p2.pop_back();
  134. p1.push_back(p2[m]);
  135. p2.erase(p2.begin() + m);
  136. tk = T(p2, t, -1);
  137. }
  138. }
  139. }
  140. return {p2, p1};
  141. }
  142.  
  143. pair<vector<int>, vector<int>> SecondModification()
  144. {
  145. int tk = t;
  146. vector<int> p1, p2;
  147. queue<int> nk;
  148. for (int i = 0; i < n; i++)
  149. nk.push(i);
  150. while (!nk.empty()) {
  151. int i = nk.front();
  152. tk = max(tk, r[i]);
  153. if (tk + p[i] <= d[i])
  154. {
  155. p2.push_back(i);
  156. tk += p[i];
  157. nk.pop();
  158. } else {
  159. int ind1 = 0;
  160. int ind2 = 0;
  161. p2.push_back(i);
  162.  
  163. vector<int> cand(p2.size());
  164. for (int j = 0; j < p2.size(); j++) {
  165. cand[j] = T(p2, t, p2[j]);
  166. if (cand[ind1] > cand[j]) {
  167. ind1 = j;
  168. }
  169. if (w[p2[ind2]]> w[p2[j]]) {
  170. ind2 = j;
  171. }
  172. }
  173. int m = -1;
  174. for (int j = 0; j < p2.size(); j++) {
  175. if (cand[j]== cand[ind1] && w[p2[j]] == w[p2[ind2]]) {
  176. m = j;
  177. }
  178. }
  179. if (m == -1)
  180. {
  181. m = ind2;
  182. for (int j = 0; j < p2.size(); j++) {
  183. if (w[p2[j]]== w[p2[m]] &&
  184. cand[j]< cand[m]) {
  185. m = j;
  186. }
  187. }
  188. }
  189. if (p2[m] == i) {
  190. p2.pop_back();
  191. p1.push_back(i);
  192. nk.pop();
  193. } else {
  194. p2.pop_back();
  195. p1.push_back(p2[m]);
  196. p2.erase(p2.begin() + m);
  197. tk = T(p2, t, -1);
  198. }
  199. }
  200. }
  201. return {p2, p1};
  202. }
  203. void display(ostream &cout) {
  204. cout << "N = " << n << " t = " << t << endl;
  205. cout << "r = ";
  206. for (int i = 0; i < n; i++)
  207. cout << r[i] << " ";
  208. cout << endl;
  209. cout << "d = ";
  210. for (int i = 0; i < n; i++)
  211. cout << d[i] << " ";
  212. cout << endl;
  213. cout << "p = ";
  214. for (int i = 0; i < n; i++)
  215. cout << p[i] << " ";
  216. cout << endl;
  217. cout << "w = ";
  218. for (int i = 0; i < n; i++)
  219. cout << w[i] << " ";
  220. cout << endl;
  221. }
  222.  
  223. void solve(int disp, ostream &cout) {
  224. auto firstResult = FirstModification();
  225. cout << "Результат первой модификации";
  226. cout << endl << "Запаздывающие требования: ";
  227. for (auto elem : firstResult.second)
  228. cout << elem + 1 << " ";
  229.  
  230. cout << endl << "Незапаздывающие требования: ";
  231. for (auto elem : firstResult.first)
  232. cout << elem + 1 << " ";
  233.  
  234. cout << endl <<"Сумма весов запаздывающих требований: " << F(firstResult.second) << endl << endl;
  235. if (disp) {
  236. cerr << "Результат первой модификации" << endl;
  237. cerr << "Запаздывающие требования: ";
  238. for (auto it : firstResult.second)
  239. cerr << it + 1 << " ";
  240.  
  241. cerr << endl << "Незапаздывающие требования: ";
  242. for (auto it : firstResult.first)
  243. cerr << it + 1 << " ";
  244. cerr << endl<<"Сумма весов запаздывающих требований: " << F(firstResult.second) <<
  245. endl << endl;
  246. }
  247. auto secondResult = SecondModification();
  248. cout << "Результат второй модификации";
  249.  
  250. cout << endl << "Запаздывающие требования: ";
  251. for (auto it : secondResult.second)
  252. cout << it + 1 << " ";
  253.  
  254. cout << endl <<"Незапаздывающие требования: ";
  255. for (auto it : secondResult.first)
  256. cout << it + 1 << " ";
  257.  
  258. cout << endl<<"Сумма весов запаздывающих требований: " << F(secondResult.second) << endl <<
  259. endl;
  260. if (disp) {
  261. cerr << "Результат второй модификации";
  262. cerr << endl << "Запаздывающие требования: ";
  263. for (auto it : secondResult.second)
  264. cerr << it + 1 << " ";
  265.  
  266. cerr << endl << "Незапаздывающие требования: ";
  267. for (auto it : secondResult.first)
  268. cerr << it + 1 << " ";
  269. cerr << endl<<"Сумма весов запаздывающих требований: " << F(secondResult.second) <<
  270. endl << endl;
  271. }
  272. }
  273. random_device rd;
  274.  
  275. int gen_num(int l, int r)
  276. {
  277. return l + rd()%(r-l + 1);
  278. }
  279. void generate_data(int count, int LborderR, int RborderR, int LborderD, int RborderD, int LborderW, int RborderW) // генерация t,r,d,p,w
  280. {
  281. n = count;
  282. t = rd() % 10;
  283. r.resize(n);
  284. d.resize(n);
  285. p.resize(n);
  286. w.resize(n);
  287. for (int i = 0; i < n; i++) {
  288. d[i] = p[i] = w[i] = r[i] = 0;
  289. r[i] = gen_num(LborderR,RborderR);
  290. if (r[i]+1>RborderD)
  291. r[i]--;
  292. d[i] = gen_num(r[i]+1,RborderD);
  293. while (d[i] <= r[i])
  294. d[i] = gen_num(LborderD,RborderD);
  295. w[i] = gen_num(LborderW,RborderW);
  296. }
  297.  
  298. sort(d.begin(), d.end());
  299. sort(r.begin(), r.end());
  300. for (int i = 0; i < n; i++)
  301. p[i] = rd() % (d[i] - r[i]) + 1;
  302. sort(d.begin(), d.end());
  303. }
  304. int best[2];
  305. double min_sootn[2];
  306. double max_sootn[2];
  307. void solveandcompare(int disp, ostream &cout) {
  308. auto firstResult = FirstModification();
  309. cout << "Результат первой модификации";
  310. cout << endl << "Запаздывающие требования: ";
  311. for (auto elem : firstResult.second)
  312. cout << elem + 1 << " ";
  313.  
  314. cout << endl << "Незапаздывающие требования: ";
  315. for (auto elem : firstResult.first)
  316. cout << elem + 1 << " ";
  317.  
  318. cout << endl << "Сумма весов запаздывающих требований: " << F(firstResult.second) << endl << endl;
  319. if (disp) {
  320. cerr << "Результат первой модификации" << endl;
  321. cerr << "Запаздывающие требования: ";
  322. for (auto it : firstResult.second)
  323. cerr << it + 1 << " ";
  324.  
  325. cerr << endl << "Незапаздывающие требования: ";
  326. for (auto it : firstResult.first)
  327. cerr << it + 1 << " ";
  328. cerr << endl << "Сумма весов запаздывающих требований: " << F(firstResult.second) <<
  329. endl << endl;
  330. }
  331. auto secondResult = SecondModification();
  332. cout << "Результат второй модификации";
  333.  
  334. cout << endl << "Запаздывающие требования: ";
  335. for (auto it : secondResult.second)
  336. cout << it + 1 << " ";
  337.  
  338. cout << endl << "Незапаздывающие требования: ";
  339. for (auto it : secondResult.first)
  340. cout << it + 1 << " ";
  341.  
  342. cout << endl << "Сумма весов запаздывающих требований: " << F(secondResult.second) << endl <<
  343. endl;
  344. if (disp) {
  345. cerr << "Результат второй модификации";
  346. cerr << endl << "Запаздывающие требования: ";
  347. for (auto it : secondResult.second)
  348. cerr << it + 1 << " ";
  349.  
  350. cerr << endl << "Незапаздывающие требования: ";
  351. for (auto it : secondResult.first)
  352. cerr << it + 1 << " ";
  353. cerr << endl << "Сумма весов запаздывающих требований: " << F(secondResult.second) <<
  354. endl << endl;
  355. }
  356.  
  357. int first = F(firstResult.second);
  358. int second = F(secondResult.second);
  359. if (first <= second) {
  360. best[0]++;
  361. }
  362. if (second <= first) {
  363. best[1]++;
  364. }
  365. if (first != 0 && second != 0) {
  366. min_sootn[0] = min(min_sootn[0], first * 1.0 / second);
  367. max_sootn[0] = max(max_sootn[0], first * 1.0 / second);
  368.  
  369. min_sootn[1] = min(min_sootn[1], second * 1.0 / first);
  370. max_sootn[1] = max(max_sootn[1], second * 1.0 / first);
  371. }
  372. }
  373. void Compare_Modifications(ofstream &fout) {
  374. cerr << "Введите количество задач, на которых будут сравниваться алгоритмы: ";
  375. int tests;
  376. cin >> tests;
  377. cerr << "Введите n: ";
  378. int count;
  379. cin >> count;
  380. int rborderR, rborderD, rborderW;
  381. int lborderR, lborderD, lborderW;
  382. cerr << "Введите нижнюю и верхнюю границу r_i: ";
  383. cin >> lborderR >> rborderR;
  384. cerr << "Введите нижнюю и верхнюю границу d_i: ";
  385. cin >> lborderD >> rborderD;
  386. cerr << "Введите нижнюю и верхнюю границу w_i: ";
  387. cin >> lborderW >> rborderW;
  388. int disp = 2;
  389. while (disp != 0 && disp != 1) {
  390. cerr << "Для просмотра решения каждого примера нажмите 1, иначе 0: ";
  391. cin >> disp;
  392. }
  393.  
  394. best[0] = best[1] = 0;
  395. max_sootn[0] = max_sootn[1] = 0;
  396. min_sootn[0] = min_sootn[1] = 1e9;
  397.  
  398. for (int i = 0; i < tests; i++) {
  399. generate_data(count, lborderR, rborderR, lborderD, rborderD, lborderW, rborderW);
  400. display(fout);
  401. if (disp)
  402. display(cerr);
  403. solveandcompare(disp, fout);
  404. }
  405. cerr << fixed << setprecision(2) << "Первая модификация не хуже второй в " << best[0] * 100.0 / tests << " % задач"
  406. << endl;
  407. cerr << fixed << setprecision(2) << "Вторая модификация не хуже первой в " << best[1] * 100.0 / tests << " % задач"
  408. << endl;
  409. fout << fixed << setprecision(2) << "Первая модификация не хуже второй в " << best[0] * 100.0 / tests << " % задач"
  410. << endl;
  411. fout << fixed << setprecision(2) << "Вторая модификация не хуже первой в " << best[1] * 100.0 / tests << " % задач"
  412. << endl;
  413.  
  414.  
  415. cerr << fixed << setprecision(2) << "Минимальное соотношение первой модификации ко второй " << min_sootn[0] << endl;
  416. cerr << fixed << setprecision(2) << "Максимальное соотношение первой модификации ко второй " << max_sootn[0]
  417. << endl;
  418. cerr << fixed << setprecision(2) << "Минимальное соотношение второй модификации к первой " << min_sootn[1] << endl;
  419. cerr << fixed << setprecision(2) << "Максимальное соотношение второй модификации к первой " << max_sootn[1] << endl;
  420.  
  421.  
  422. fout << fixed << setprecision(2) << "Минимальное соотношение первой модификации ко второй " << min_sootn[0] << endl;
  423. fout << fixed << setprecision(2) << "Максимальное соотношение первой модификации ко второй " << max_sootn[0]
  424. << endl;
  425. fout << fixed << setprecision(2) << "Минимальное соотношение второй модификации к первой " << min_sootn[1] << endl;
  426. fout << fixed << setprecision(2) << "Максимальное соотношение второй модификации к первой " << max_sootn[1] << endl;
  427.  
  428.  
  429. if (best[0] > best[1]) {
  430. double proc = 100;
  431. if (best[1]) {
  432. proc = best[0] * 1.0 / best[1];
  433. }
  434. cerr
  435. << "Для заданных ограничений лучше выбрать первую модификацию, так как она отработала лучше второй модификации в "
  436. << proc << "% задач" << endl;
  437. fout
  438. << "Для заданных ограничений лучше выбрать первую модификацию, так как она отработала лучше второй модификации в "
  439. << proc << "% задач" << endl;
  440.  
  441. } else {
  442. double proc = 100;
  443. if (best[0]) {
  444. proc = best[1] * 1.0 / best[0];
  445. }
  446.  
  447. cerr
  448. << "Для заданных ограничений лучше выбрать вторую модификацию, так как она отработала лучше первой модификации в "
  449. <<
  450. proc << "% задач" << endl;
  451.  
  452. fout
  453. << "Для заданных ограничений лучше выбрать вторую модификацию, так как она отработала лучше первой модификации в "
  454. <<
  455. proc << "% задач" << endl;
  456.  
  457. }
  458. }
  459.  
  460.  
  461. int main()
  462. {
  463. ofstream fout("result.txt");
  464. bool ok = true;
  465. while (ok) {
  466. int choise = -1;
  467. while (choise < 0 || choise > 3) {
  468. cerr << "Введите 1 для ввода данных с файла" << endl;
  469. cerr << "2 для ввода данных с консоли" << endl;
  470. cerr << "3 для сравнения модификаций" << endl;
  471. cerr << "0 для выхода из программы" << endl;
  472. cin >> choise;
  473.  
  474. }
  475. switch (choise) {
  476. case 1: {
  477. cerr << "Введите путь к файлу: ";
  478. string path;
  479. cin >> path;
  480. if (!FileInput(path)) {
  481. cerr << "Не удалось открыть файл или неверные входные данные в файле" << endl;
  482. } else {
  483. display(cerr);
  484. solve(1, fout);
  485. }
  486. break;
  487. }
  488. case 2: {
  489. ConsoleInput();
  490. solve(1, fout);
  491. break;
  492. }
  493. case 3:{
  494. Compare_Modifications(fout);
  495. break;
  496. }
  497. case 0: {
  498. ok = false;
  499. break;
  500. }
  501. }
  502. }
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement