Dmaxiya

数据生成参考代码

Dec 11th, 2025
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 100000 + 100;
  6. const int testCases = 50;
  7. int n;
  8. int num[maxn];
  9. ofstream fout;
  10.  
  11. string numToStr(int x) {
  12.     string str;
  13.     while (x != 0) {
  14.         str = char('0' + x % 10) + str;
  15.         x /= 10;
  16.     }
  17.     return str;
  18. }
  19.  
  20. string getFileInName(int index) {
  21.     return "data/" + numToStr(index) + ".in";
  22. }
  23.  
  24. string getFileOutName(int index) {
  25.     return "data/" + numToStr(index) + ".out";
  26. }
  27.  
  28. LL getRand(LL l, LL r) {
  29.     LL rnd = rand();
  30.     rnd = (rnd << 15) | rand();
  31.     rnd = (rnd << 15) | rand();
  32.     rnd = (rnd << 15) | rand();
  33.     return rnd % (r - l + 1) + l;
  34. }
  35.  
  36. void output(int fileIdx) {
  37.     string fileInName = getFileInName(fileIdx);
  38.     fout.open(fileInName);
  39.     fout << n << endl;
  40.     for (int i = 1; i <= n; ++i) {
  41.         if (i != 1) {
  42.             fout << " ";
  43.         }
  44.         fout << num[i];
  45.     }
  46.     fout << endl;
  47.     fout.close();
  48.     string fileOutName = getFileOutName(fileIdx);
  49.     system(("std.exe < " + fileInName + " > " + fileOutName).c_str());
  50. }
  51.  
  52. void genData(int n, int l, int r) {
  53.     for (int i = 1; i <= n; ++i) {
  54.         num[i] = getRand(l, r);
  55.     }
  56. }
  57.  
  58. void writeToFile(int from, int to, int nl, int nr) {
  59.     int e9 = 1000000000;
  60.     n = getRand(nl, nr);
  61.     genData(n, 1, e9);
  62.     output(from);
  63.     n = getRand(nl, nr);
  64.     genData(n, -e9, -1);
  65.     output(from + 1);
  66.     n = getRand(nl, nr);
  67.     genData(n, -e9, e9);
  68.     output(from + 2);
  69.     for (int i = from + 3; i <= to; ++i) {
  70.         n = getRand(nl, nr);
  71.         genData(n, -e9, e9);
  72.         output(i);
  73.     }
  74. }
  75.  
  76. int main() {
  77.     ios::sync_with_stdio(false);
  78.     srand(time(0));
  79.  
  80.     // data in 60%, 1 <= n <= 1000
  81.     int one = testCases * 60 / 100;
  82.     writeToFile(1, one, 1, 1000);
  83.  
  84.     // data in 100%, 1 <= n <= 100000
  85.     writeToFile(one + 1, testCases, 90000, 100000);
  86.  
  87.     return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment