pku acm 1033

來源:互聯網
上載者:User
//WA 了3次才AC,題目不難,但交的人不多,思想很簡單,直接見代碼#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <functional>#include <map>using namespace std;int main(){freopen("in.txt","r",stdin);vector<int> iv_s,//cluster sourseiv_d;//cluster destinationint N,K; // 1 <= K < N <= 10000int si,clu;int sum = 0;cin>>N>>K;char* flg = new char[N+2];memset(flg,'0',N+2);flg[N+1] = '\0';int index = 1;bool bval = true;while(K--){cin>>si;while(si--){cin>>clu;if(clu != index)//本來就在期望位置的就不需要加入移動簇表中了{bval = false;iv_s.push_back(clu);iv_d.push_back(index);}flg[clu]='1';++index;}}sum = index-1;if(bval == true){cout<<"No optimization needed";return 0;}//cout<<&flg[1]<<endl;//cout<<"sum = "<<sum<<endl;//copy(iv_s.begin(),iv_s.end(),ostream_iterator<int>(cout," "));//cout<<endl;//copy(iv_d.begin(),iv_d.end(),ostream_iterator<int>(cout," "));//cout<<endl<<endl;{int step = 0;int i = 0;while(iv_s.size() > 0){bool bval = false;for(i = 0; i < iv_s.size();){if(flg[ iv_d[i] ] == '0'){cout<<iv_s[i]<<" "<<iv_d[i]<<endl;flg[ iv_d[i] ] = '1';flg[ iv_s[i] ] = '0';bval = true;iv_d.erase(iv_d.begin()+i);//迭代器的使用iv_s.erase(iv_s.begin()+i);//找到自己的位置後,從簇表中刪除++step;}else++i;}if(bval == false)//存在死鏈,必須依賴空閑塊進行交換{for(int i = 1; i <= N; i++)if(flg[i] == '0')//找到一個空閑塊{cout<<iv_s[0]<<" "<<i<<endl;flg[ iv_s[0] ] = '0';iv_s[0] = i;flg[i] = '1';++step;break;}}}//cout<<endl<<"step = "<<step<<endl;}return 1;}

測試資料:

20 3
4 2 3 11 12
1 7
3 18 5 10

8 3
3 3 4 5
3 1 2 7
1 8

6 1
3 6 3 1

4 1
3 3 2 4

8 3
3 1 2 3
3 4 5 6
1 7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.