zoukankan      html  css  js  c++  java
  • UVA 512 Spreadsheet Tracking

    题目链接:https://vjudge.net/problem/UVA-512

    题目翻译摘自《算法禁赛入门经典》

    题目大意

      有一个 r 行 c 列(1 ≤ r,c ≤ 50)的电子表格,行从上到下编号为 1~r,列从左到右编号为 1~c。

      现在有五种操作,(删除/插入)某些(行/列)及交换两个单元格位置。

      现在对表格进行 n 次操作,接下来是 q 个查询,每个查询格式 为“r c”,表示查询原始表格的单元格 (r,c)。对于每个查询,输出操作执行完后该单元格的新位置。输入保证在任意时刻行列数均不超过 50。

    分析

      模拟,体力活,考察基本功。

    代码如下

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3  
      4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
      5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
      6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
      7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
      8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
      9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     12  
     13 #define pr(x) cout << #x << " = " << x << "  "
     14 #define prln(x) cout << #x << " = " << x << endl
     15  
     16 #define LOWBIT(x) ((x)&(-x))
     17  
     18 #define ALL(x) x.begin(),x.end()
     19 #define INS(x) inserter(x,x.begin())
     20  
     21 #define ms0(a) memset(a,0,sizeof(a))
     22 #define msI(a) memset(a,inf,sizeof(a))
     23 #define msM(a) memset(a,-1,sizeof(a))
     24 
     25 #define MP make_pair
     26 #define PB push_back
     27 #define ft first
     28 #define sd second
     29  
     30 template<typename T1, typename T2>
     31 istream &operator>>(istream &in, pair<T1, T2> &p) {
     32     in >> p.first >> p.second;
     33     return in;
     34 }
     35  
     36 template<typename T>
     37 istream &operator>>(istream &in, vector<T> &v) {
     38     for (auto &x: v)
     39         in >> x;
     40     return in;
     41 }
     42  
     43 template<typename T1, typename T2>
     44 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     45     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     46     return out;
     47 }
     48 
     49 inline int gc(){
     50     static const int BUF = 1e7;
     51     static char buf[BUF], *bg = buf + BUF, *ed = bg;
     52     
     53     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
     54     return *bg++;
     55 } 
     56 
     57 inline int ri(){
     58     int x = 0, f = 1, c = gc();
     59     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
     60     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
     61     return x*f;
     62 }
     63 
     64 template<class T>
     65 inline string toString(T x) {
     66     ostringstream sout;
     67     sout << x;
     68     return sout.str();
     69 }
     70  
     71 typedef long long LL;
     72 typedef unsigned long long uLL;
     73 typedef pair< double, double > PDD;
     74 typedef pair< int, int > PII;
     75 typedef pair< int, PII > PIPII;
     76 typedef pair< string, int > PSI;
     77 typedef pair< int, PSI > PIPSI;
     78 typedef set< int > SI;
     79 typedef set< PII > SPII;
     80 typedef vector< int > VI;
     81 typedef vector< VI > VVI;
     82 typedef vector< PII > VPII;
     83 typedef map< int, int > MII;
     84 typedef map< int, PII > MIPII;
     85 typedef map< PII, int > MPIII;
     86 typedef map< string, int > MSI;
     87 typedef map< string, string > MSS;
     88 typedef map< PII, string > MPIIS;
     89 typedef map< PII, PII > MPIIPII;
     90 typedef multimap< int, int > MMII;
     91 //typedef unordered_map< int, int > uMII;
     92 typedef pair< LL, LL > PLL;
     93 typedef vector< LL > VL;
     94 typedef vector< VL > VVL;
     95 typedef priority_queue< int > PQIMax;
     96 typedef priority_queue< int, VI, greater< int > > PQIMin;
     97 const double EPS = 1e-6;
     98 const LL inf = 0x7fffffff;
     99 const LL infLL = 0x7fffffffffffffffLL;
    100 const LL mod = 1e9 + 7;
    101 const int maxN = 1e4 + 7;
    102 const LL ONE = 1;
    103 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
    104 const LL oddBits = 0x5555555555555555;
    105 
    106 int r, c, n, T;
    107 string str; 
    108 PII sheet[57][57];
    109 MPIIPII mpp;
    110 MSI op = {MP("DR", 0), MP("DC", 1), MP("IR", 2), MP("IC", 3), MP("EX", 4)};
    111 
    112 void printSheet() {
    113     For(i, 1, r) 
    114         For(j, 1, c) 
    115             cout << "[" << sheet[i][j].ft << ", " << sheet[i][j].sd << "]" << " 
    "[j == c];
    116 }
    117 
    118 // 将第 x 行替换成第 y 行 
    119 void replaceRow(int x, int y) {
    120     if(x == y) return;
    121     For(j, 1, c) {
    122         mpp[sheet[y][j]] = MP(x, j);
    123         sheet[x][j] = sheet[y][j];
    124     }
    125 }
    126 
    127 // 将第 x 列替换成第 y 列
    128 void replaceCol(int x, int y) {
    129     if(x == y) return;
    130     For(i, 1, r) {
    131         mpp[sheet[i][y]] = MP(i, x);
    132         sheet[i][x] = sheet[i][y];
    133     }
    134 }
    135 
    136 void removeRow(int x) {
    137     For(j, 1, c) {
    138         mpp[sheet[x][j]] = MP(-1, -1);
    139         sheet[x][j] = MP(-1, -1);
    140     }
    141 }
    142 
    143 void removeCol(int x) {
    144     For(i, 1, r) {
    145         mpp[sheet[i][x]] = MP(-1, -1);
    146         sheet[i][x] = MP(-1, -1);
    147     }
    148 }
    149 
    150 void insertRow(int x) {
    151     For(j, 1, c) sheet[x][j] = MP(-1, -1);    
    152 }
    153 
    154 void insertCol(int x) {
    155     For(i, 1, r) sheet[i][x] = MP(-1, -1);
    156 }
    157 
    158 void DR() {
    159     int n, x, newr = 0;
    160     SI si;
    161     cin >> n;
    162     Rep(i, n) {
    163         cin >> x;
    164         si.insert(x);
    165     }
    166     For(i, 1, r) {
    167         if(si.find(i) != si.end()) removeRow(i);
    168         else replaceRow(++newr, i);
    169     }
    170     r = newr;
    171 }
    172 
    173 void DC() {
    174     int n, x, newc = 0;
    175     SI si;
    176     cin >> n;
    177     Rep(i, n) {
    178         cin >> x;
    179         si.insert(x);
    180     }
    181     For(i, 1, c) {
    182         if(si.find(i) != si.end()) removeCol(i);
    183         else replaceCol(++newc, i);
    184     }
    185     c = newc;
    186 }
    187 
    188 void IR() {
    189     int n, x, newr;
    190     SI si;
    191     cin >> n;
    192     Rep(i, n) {
    193         cin >> x;
    194         si.insert(x);
    195     }
    196     newr = r + si.size();
    197     rFor(i, r, 1) {
    198         replaceRow(newr--, i);
    199         if(si.find(i) != si.end()) insertRow(newr--);
    200     }
    201     r = r + si.size();
    202 }
    203 
    204 void IC() {
    205     int n, x, newc;
    206     SI si;
    207     cin >> n;
    208     Rep(i, n) {
    209         cin >> x;
    210         si.insert(x);
    211     }
    212     newc = c + si.size();
    213     rFor(i, c, 1) {
    214         replaceCol(newc--, i);
    215         if(si.find(i) != si.end()) insertCol(newc--);
    216     }
    217     c = c + si.size();
    218 }
    219 
    220 void EX() {
    221     int x, y, a, b;
    222     cin >> x >> y >> a >> b;
    223     swap(sheet[x][y], sheet[a][b]);
    224     mpp[sheet[x][y]] = MP(x, y);
    225     mpp[sheet[a][b]] = MP(a, b);
    226 }
    227 
    228 void (*func[5])() = {DR, DC, IR, IC, EX};
    229 
    230 int main(){
    231     //freopen("MyOutput.txt","w",stdout);
    232     //freopen("input.txt","r",stdin);
    233     INIT();
    234     while(cin >> r >> c) {
    235         if(r == 0) break;
    236         mpp.clear();
    237         For(i, 1, r) {
    238             For(j, 1, c) {
    239                 sheet[i][j] = MP(i, j);
    240                 mpp[MP(i, j)] = MP(i, j);
    241             }
    242         }
    243         
    244         cin >> n;
    245         Rep(i, n) {
    246             cin >> str;
    247             func[op[str]]();
    248             //prln(str);
    249             //printSheet();
    250         }
    251         
    252         if(T) cout << endl;
    253         cout << "Spreadsheet #" << ++T << endl;
    254         cin >> n;
    255         Rep(i, n) {
    256             int x, y;
    257             cin >> x >> y;
    258             cout << "Cell data in (" << x << "," << y;
    259             if(mpp[MP(x, y)] == MP(-1, -1)) cout << ") GONE
    ";
    260             else cout << ") moved to (" << mpp[MP(x, y)].ft << "," << mpp[MP(x, y)].sd << ")
    ";
    261         }
    262     }
    263     return 0;
    264 }
    View Code
  • 相关阅读:
    HDU 2546:饭卡(01背包)
    HPU 第三次积分赛:阶乘之和(水题)
    拓扑排序练习题
    HDU 2647:Reward(拓扑排序+队列)
    HDU 3342:Legal or Not(拓扑排序)
    HDU 2094:产生冠军(拓扑排序)
    POJ 2585:Window Pains(拓扑排序)
    51Nod 1002:数塔取数问题(DP)
    cogs696 longest prefix
    poj3764 The xor-longest Path
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/11028203.html
Copyright © 2011-2022 走看看