UVALive - 7000 Present Problem 简单模拟
2017-10-29 20:23
281 查看
别看了,每一列保存 排序,upper_bound 降复杂度
慢慢写吧
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
typedef long long ll;
const int maxn = 10000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int n, m, len;
vector<int> a[maxn];
int ans[maxn];
void init() {
scanf("%d %d %d", &n, &m, &len);
for(int i = 0; i <= n; ++i)
a[i].clear();
for(int i = 0; i < m; ++i) {
int r, c;
scanf("%d %d", &r, &c);
a[r].push_back(c);
}
for(int i = 0; i < n; ++i) {
sort(a[i].begin(), a[i].end());
//cout << " i " << i << " = ";
//for(int j = 0; j < a[i].size(); ++j)
// cout << a[i][j] << " ";
//cout << endl;
}
}
void solve() {
if(n == 1) {
cout << 0 << endl;
return;
}
for(int i = 0; i < n; ++i) {
ans[i] = i;
int pos = 0;
while(1) {
//cout << ans[i] << " ++= " << pos << endl;
if(ans[i] == 0) {
int t = upper_bound(a[0].begin(), a[0].end(), pos) - a[0].begin();
if(t >= a[0].size()) break;
else {
pos = a[0][t];
ans[i] = 1;
}
} else if(ans[i] == n-1) {
int t = upper_bound(a[n-2].begin(), a[n-2].end(), pos) - a[n-2].begin();
if(t >= a[n-2].size()) break;
else {
//cout << t << " " << a[n-2][t] << " ==== 23333333" << endl;
pos = a[n-2][t];
ans[i] = ans[i]-1;
}
} else {
int t1 = upper_bound(a[ans[i]-1].begin(), a[ans[i]-1].end(), pos) - a[ans[i]-1].begin();
int t2 = upper_bound(a[ans[i]].begin(), a[ans[i]].end(), pos) - a[ans[i]].begin();
if(t1 < a[ans[i]-1].size() && t2 < a[ans[i]].size()) {
if(a[ans[i]-1][t1] < a[ans[i]][t2]) {pos = a[ans[i]-1][t1];
ans[i] = ans[i]-1;
}
else {//cout << a[ans[i]-1][t1] << " ------------------------------ " << a[ans[i]][t2] << endl;
pos = a[ans[i]][t2];
ans[i] = ans[i]+1;
//cout << ans[i] << " +++ 4654765 " << pos << endl;
}
//cout << " 2333 " << endl;
}
else if(t1 < a[ans[i]-1].size()) {
pos = a[ans[i]-1][t1];
ans[i] = ans[i]-1; //cout << " ++++++++++++++++++++++++ " << endl;
}
else if(t2 < a[ans[i]].size()) {pos = a[ans[i]][t2];
ans[i] = ans[i]+1; //cout << " +++++++==============+++++++++++ " << endl;
}
else {
break;
}
}
}
//cout << ans[i] << endl;
printf("%d\n", ans[i]);
}
}
int main() {
//int b[4] = {0,1,1,2};
//int tt = upper_bound(b,b+4, 5) - b;
//cout << tt << endl;
int T;
scanf("%d", &T);
while(T--) {
init();
solve();
}
return 0;
}
慢慢写吧
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
typedef long long ll;
const int maxn = 10000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int n, m, len;
vector<int> a[maxn];
int ans[maxn];
void init() {
scanf("%d %d %d", &n, &m, &len);
for(int i = 0; i <= n; ++i)
a[i].clear();
for(int i = 0; i < m; ++i) {
int r, c;
scanf("%d %d", &r, &c);
a[r].push_back(c);
}
for(int i = 0; i < n; ++i) {
sort(a[i].begin(), a[i].end());
//cout << " i " << i << " = ";
//for(int j = 0; j < a[i].size(); ++j)
// cout << a[i][j] << " ";
//cout << endl;
}
}
void solve() {
if(n == 1) {
cout << 0 << endl;
return;
}
for(int i = 0; i < n; ++i) {
ans[i] = i;
int pos = 0;
while(1) {
//cout << ans[i] << " ++= " << pos << endl;
if(ans[i] == 0) {
int t = upper_bound(a[0].begin(), a[0].end(), pos) - a[0].begin();
if(t >= a[0].size()) break;
else {
pos = a[0][t];
ans[i] = 1;
}
} else if(ans[i] == n-1) {
int t = upper_bound(a[n-2].begin(), a[n-2].end(), pos) - a[n-2].begin();
if(t >= a[n-2].size()) break;
else {
//cout << t << " " << a[n-2][t] << " ==== 23333333" << endl;
pos = a[n-2][t];
ans[i] = ans[i]-1;
}
} else {
int t1 = upper_bound(a[ans[i]-1].begin(), a[ans[i]-1].end(), pos) - a[ans[i]-1].begin();
int t2 = upper_bound(a[ans[i]].begin(), a[ans[i]].end(), pos) - a[ans[i]].begin();
if(t1 < a[ans[i]-1].size() && t2 < a[ans[i]].size()) {
if(a[ans[i]-1][t1] < a[ans[i]][t2]) {pos = a[ans[i]-1][t1];
ans[i] = ans[i]-1;
}
else {//cout << a[ans[i]-1][t1] << " ------------------------------ " << a[ans[i]][t2] << endl;
pos = a[ans[i]][t2];
ans[i] = ans[i]+1;
//cout << ans[i] << " +++ 4654765 " << pos << endl;
}
//cout << " 2333 " << endl;
}
else if(t1 < a[ans[i]-1].size()) {
pos = a[ans[i]-1][t1];
ans[i] = ans[i]-1; //cout << " ++++++++++++++++++++++++ " << endl;
}
else if(t2 < a[ans[i]].size()) {pos = a[ans[i]][t2];
ans[i] = ans[i]+1; //cout << " +++++++==============+++++++++++ " << endl;
}
else {
break;
}
}
}
//cout << ans[i] << endl;
printf("%d\n", ans[i]);
}
}
int main() {
//int b[4] = {0,1,1,2};
//int tt = upper_bound(b,b+4, 5) - b;
//cout << tt << endl;
int T;
scanf("%d", &T);
while(T--) {
init();
solve();
}
return 0;
}
相关文章推荐
- UVALive 2052 Number Steps【简单模拟】水题
- Working Hours UVALive - 7017(简单模拟)
- UValive 7269 (模拟)
- UVALive 6091 并查集简单应用
- UVA - 10716 - Evil Straw Warts Live (简单模拟)
- UVALive 6858 Frame (模拟)
- UVALive 7139 Rotation(模拟)
- UVALive - 7511 Multiplication Table (模拟)
- UVALive4886 E - Page Count 模拟
- UVALive 7512 November 11th(模拟, 思维)
- UVA 340 Master-Mind Hints 猜数字提示的游戏 简单模拟
- UVALive 4119 Always an integer (差分数列,模拟)
- UVaLive LA 4256 | UVa 1424 - Salesmen(简单DP)
- [大模拟]两种方式解决Chat(UVALive-7047)大模拟
- UVaLive/LA 6805 Pantun Grader(模拟)
- UVaLive 3530 - Martian Mining (简单DP)
- 简单几何(圆与多边形公共面积) UVALive 7072 Signal Interference (14广州D)
- UVaLive 6802 Turtle Graphics (水题,模拟)
- UVALive 7146 Defeat the Enemy(模拟)
- UVALive - 7511 Multiplication Table (模拟)