problemscpp
A collection of my answers to algorithm problems in c++.
函数
acwing::acwing3402 命名空间参考

  1. 等差数列
更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (acwing3402, case1)
 

详细描述

  1. 等差数列

函数说明

◆ main()

int acwing::acwing3402::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing408.cpp892 行定义.

892 {
893 int n, m;
894 cin >> n >> m;
895 vector<vector<int>> a(n + 1, vector<int>(m + 1, 0));
896 unordered_set<int> filled_rows = unordered_set<int>();
897 unordered_set<int> filled_cols = unordered_set<int>();
898 vector<int> row_cnt = vector<int>(n + 1, 0);
899 vector<int> col_cnt = vector<int>(m + 1, 0);
900 for(int i = 1; i <= n; i++) {
901 for(int j = 1; j <= m; j++) {
902 cin >> a[i][j];
903 if(a[i][j] > 0) {
904 row_cnt[i]++;
905 col_cnt[j]++;
906 }
907 }
908 }
909 for(int i = 1; i <= n; i++) {
910 for(int j = 1; j <= m; j++) {
911 if(row_cnt[i] == n) {
912 filled_rows.insert(i);
913 }
914 if(col_cnt[j] == m) {
915 filled_cols.insert(j);
916 }
917 }
918 }
919 vector<vector<int>> a_cpy(a.begin(), a.end());
920 queue<pair<bool, int>> q = queue<pair<bool, int>>();// <is_row,index>
921 for(int i = 1; i < n; i++) {
922 if(row_cnt[i] > 1) {
923 filled_rows.insert(i);
924 q.push(make_pair(true, i));
925 }
926 }
927 for(int i = 1; i < m; i++) {
928 if(col_cnt[i] > 1) {
929 filled_cols.insert(i);
930 q.push(make_pair(false, i));
931 }
932 }
933 while(!q.empty()) {
934 auto [is_row, index] = q.front();
935 q.pop();
936 if(is_row) {
937 int l = 0;
938 int r = 0;
939 for(int j = 1; j <= m; j++) {
940 if(a[index][j] > 0) {
941 if(l == 0) {
942 l = j;
943 } else if(r == 0) {
944 r = j;
945 } else {
946 break;
947 }
948 }
949 }
950 int d = (a[index][r] - a[index][l]) / (r - l);
951 for(int j = 1; j <= m; j++) {
952 if(a[index][j] == 0) {
953 a[index][j] = a[index][l] + (j - l) * d;
954 col_cnt[j]++;
955 if(col_cnt[j] > 1 && filled_cols.find(j) == filled_cols.end()) {
956 filled_cols.insert(j);
957 q.emplace(false, j);
958 }
959 }
960 }
961 } else {
962 int l = 0;
963 int r = 0;
964 for(int i = 1; i <= n; i++) {
965 if(a[i][index] > 0) {
966 if(l == 0) {
967 l = i;
968 } else if(r == 0) {
969 r = i;
970 } else {
971 break;
972 }
973 }
974 }
975 int d = (a[r][index] - a[l][index]) / (r - l);
976 for(int i = 1; i <= n; i++) {
977 if(a[i][index] == 0) {
978 a[i][index] = a[l][index] + (i - l) * d;
979 row_cnt[i]++;
980 if(row_cnt[i] > 1 && filled_rows.find(i) == filled_rows.end()) {
981 filled_rows.insert(i);
982 q.emplace(true, i);
983 }
984 }
985 }
986 }
987 }
988 for(int i = 1; i <= n; i++) {
989 for(int j = 1; j <= m; j++) {
990 if(a_cpy[i][j] != a[i][j]) {
991 cout << i << ' ' << j << ' ' << a[i][j] << endl;
992 }
993 }
994 }
995 return 0;
996 }

被这些函数引用 TEST().

◆ TEST()

acwing::acwing3402::TEST ( acwing3402  ,
case1   
)

在文件 acwing408_test.cpp1282 行定义.

1282 {
1283 istringstream in("3 4\n"
1284 "1 2 0 0\n"
1285 "0 0 0 0\n"
1286 "3 0 0 0");
1287 auto out = ostringstream();
1288 main(in, out);
1289 const auto ans = out.str();
1290 ASSERT_EQ("1 3 3\n"
1291 "1 4 4\n"
1292 "2 1 2\n",
1293 ans);
1294 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().