思维题 UVA 10881 Piotr's Ants
生活随笔
收集整理的這篇文章主要介紹了
思维题 UVA 10881 Piotr's Ants
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
題目傳送門
1 /* 2 題意:在坐標軸上一群螞蟻向左或向右爬,問經過ts后,螞蟻的位置和狀態 3 思維題:本題的關鍵1:螞蟻相撞看作是對穿過去,那么只要判斷誰是誰就可以了 4 關鍵2:螞蟻的相對位置不變 關鍵3:order數組記錄順序 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <iostream> 9 #include <cstring> 10 #include <string> 11 #include <cmath> 12 using namespace std; 13 14 const int MAXN = 1e4 + 10; 15 const int INF = 0x3f3f3f3f; 16 const char dir_name[][10] = {"L", "Turning", "R"}; 17 struct Ant 18 { 19 int pos, dir, id; 20 bool operator < (const Ant &a) const 21 { 22 return pos < a.pos; 23 } 24 }pre[MAXN], now[MAXN]; 25 int order[MAXN]; 26 27 int main(void) //UVA 10881 Piotr's Ants 28 { 29 // freopen ("UVA_10881.in", "r", stdin); 30 31 int T; int cas = 0; int len, t, n; 32 scanf ("%d", &T); 33 while (T--) 34 { 35 scanf ("%d%d%d", &len, &t, &n); 36 char ch; 37 for (int i=1; i<=n; ++i) 38 { 39 int p, d; char ch; 40 scanf ("%d %c", &p, &ch); 41 d = ((ch=='L') ? -1 : 1); 42 pre[i] = (Ant) {p, d, i}; 43 now[i] = (Ant) {p+t*d, d, 0}; 44 } 45 46 sort (pre+1, pre+1+n); //計算相對位置 47 for (int i=1; i<=n; ++i) order[pre[i].id] = i; //輸入(輸出)的順序 48 49 sort (now+1, now+1+n); 50 for (int i=1; i<n; ++i) 51 { 52 if (now[i].pos == now[i+1].pos) 53 now[i].dir = now[i+1].dir = 0; 54 } 55 56 printf ("Case #%d:\n", ++cas); 57 for (int i=1; i<=n; ++i) 58 { 59 int x = order[i]; 60 if (now[x].pos < 0 || now[x].pos > len) puts ("Fell off"); 61 else 62 { 63 printf ("%d %s\n", now[x].pos, dir_name[now[x].dir+1]); 64 } 65 } 66 67 puts (""); 68 } 69 70 return 0; 71 } 72 73 /* 74 Case #1: 75 2 Turning 76 6 R 77 2 Turning 78 Fell off 79 80 Case #2: 81 3 L 82 6 R 83 10 R 84 */?
轉載于:https://www.cnblogs.com/Running-Time/p/4644722.html
總結
以上是生活随笔為你收集整理的思维题 UVA 10881 Piotr's Ants的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 借增贷减是什么意思
- 下一篇: 英镑纸币面额有几种 英镑的纸币面额有哪些