uva 10881(贪心)
2015-01-28 13:12
393 查看
题意:一个长度为L的木棍上有n只蚂蚁,给出了每只蚂蚁的初始位置和朝向,然后蚂蚁开始行走,如果两只蚂蚁相撞,就立刻掉头,直到掉下木棍,问t秒后每只蚂蚁的位置和状态。
题解:因为每只蚂蚁会因为碰撞而掉头,所以相对位置是不变的,为了更容易考虑计算,那么相撞的蚂蚁可以看做是对穿而过,得到输出结果时只要按初始的相对位置再对应上不同的蚂蚁就可以了。
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 10005;
int l, T, n, order
;
struct Ant {
int dir;
int pos;
int flag;
}sta
, aft
;
char s[3][10] = {"L", "Turning", "R"};
int cmp(Ant a, Ant b) {
return a.pos < b.pos;
}
int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
char c;
scanf("%d%d%d", &l, &T, &n);
for (int i = 0; i < n; i++) {
scanf("%d %c", &sta[i].pos, &c);
sta[i].flag = i;
sta[i].dir = (c == 'L' ? -1 : 1);
aft[i].pos = sta[i].pos + T * sta[i].dir;
aft[i].dir = sta[i].dir;
}
sort(sta, sta + n, cmp);
for (int i = 0; i < n; i++)
order[sta[i].flag] = i;
sort(aft, aft + n, cmp);
for (int i = 0; i < n - 1; i++)
if (aft[i].pos == aft[i + 1].pos)
aft[i].dir = aft[i + 1].dir = 0;
printf("Case #%d:\n", cas++);
for (int i = 0; i < n; i++) {
if (aft[order[i]].pos < 0 || aft[order[i]].pos > l)
printf("Fell off\n");
else
printf("%d %s\n", aft[order[i]].pos, s[aft[order[i]].dir + 1]);
}
printf("\n");
}
return 0;
}
题解:因为每只蚂蚁会因为碰撞而掉头,所以相对位置是不变的,为了更容易考虑计算,那么相撞的蚂蚁可以看做是对穿而过,得到输出结果时只要按初始的相对位置再对应上不同的蚂蚁就可以了。
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 10005;
int l, T, n, order
;
struct Ant {
int dir;
int pos;
int flag;
}sta
, aft
;
char s[3][10] = {"L", "Turning", "R"};
int cmp(Ant a, Ant b) {
return a.pos < b.pos;
}
int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
char c;
scanf("%d%d%d", &l, &T, &n);
for (int i = 0; i < n; i++) {
scanf("%d %c", &sta[i].pos, &c);
sta[i].flag = i;
sta[i].dir = (c == 'L' ? -1 : 1);
aft[i].pos = sta[i].pos + T * sta[i].dir;
aft[i].dir = sta[i].dir;
}
sort(sta, sta + n, cmp);
for (int i = 0; i < n; i++)
order[sta[i].flag] = i;
sort(aft, aft + n, cmp);
for (int i = 0; i < n - 1; i++)
if (aft[i].pos == aft[i + 1].pos)
aft[i].dir = aft[i + 1].dir = 0;
printf("Case #%d:\n", cas++);
for (int i = 0; i < n; i++) {
if (aft[order[i]].pos < 0 || aft[order[i]].pos > l)
printf("Fell off\n");
else
printf("%d %s\n", aft[order[i]].pos, s[aft[order[i]].dir + 1]);
}
printf("\n");
}
return 0;
}
相关文章推荐
- uva 10382(贪心)
- uvalive 2949 - Elevator Stopping Plan(贪心+二分)
- UVA 11100 The Trip, 2007 贪心(输出比较奇葩)
- UVA 10718 Bit Mask 贪心+位运算
- Tokyo 2005 / UVa 1352 Colored Cubes (部分枚举&贪心)
- UVa11292 - Dragon of Loowater (贪心+模拟)
- uva 10881 - Piotr's Ants (思维,3级)
- UVALive 2326 Moving Tables 贪心
- uva11292 The Dragon of Loowater (排序+贪心)
- File Fragmentation+uva+贪心
- uva 10718 Bit Mask(位操作贪心)
- Uva1149 Bin Packing【贪心】
- UVA - 1614 Hell on the Markets 贪心+推理
- UVa 11054 Wine trading (贪心)
- EDITOR NOTTOOBAD uva+贪心
- UVA-10881 - Piotr's Ants
- uvalive 2949(二分+贪心)
- uva10718 - Bit Mask(贪心)
- uvalive4945(dp,贪心)
- UVA 12260 - Free Goodies(dp+贪心)