A. Wizards’ Duel
题意:一维坐标系,两个点以不同速度相向而行,其中一个点在原点,求相遇时相对于原点的距离。
思路:签到题。
/****************************************************
>Created Date: 2015-10-25-16.56.50
>My Soul, Your Beats!
****************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1 << 30;
const long long LINF = 1LL << 50;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const double PI = acos(-1.0);
const double eps = 1e-6;
int main(){
double l, a, b;
cin >> l >> a >> b;
double f = a / (a + b);
cout << l * f << endl;
return 0;
}
B. Rebranding
题意:给一个字符串,每次重复一种操作:字母x变y,同时y变x。问重复n次后的字符串是什么样子。
思路:在字母表中每次交换x和y的位置即可。
/****************************************************
>Created Date: 2015-10-25-16.56.50
>My Soul, Your Beats!
****************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1 << 30;
const long long LINF = 1LL << 50;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const double PI = acos(-1.0);
const double eps = 1e-6;
char a[30];
char s1[5], s2[5];
int main(){
string s;
int n, m;
for(int i = 0; i < 26; i++) a[i] = 'a' + i;
cin >> n >> m >> s;
for(int i = 0; i < m; i++){
scanf("%s %s", s1, s2);
int p1, p2;
for(int j = 0; j < 26; j++){
if(a[j] == s1[0]) p1 = j;
if(a[j] == s2[0]) p2 = j;
}
a[p1] = s2[0], a[p2] = s1[0];
}
for(int i = 0; i < s.length(); i++){
s[i] = a[s[i] - 'a'];
}
cout << s << endl;
return 0;
}
C. Median Smoothing
题意:给一个01序列A,对于Ai的值,在Ai-1,Ai,Ai+1中取0或1出现次数较多的数,序列两端的数不变。问重复多少次序列将不再变化,并输出最终序列;如果序列一直变化,输出-1。
思路:
几个规律:
1.连续出现相同数字时,这几个数字是不会改变的,这里称其为稳定点,并且稳定点有可能向两边扩散,两端的数字也是稳定点;
2.初始数列中,会变化的数字称为变动点,除去两端,不属于稳定点的点都是变动点。
由规律1知,最终状态一定稳定,不会变化。
由规律1和规律2知,稳定点向变动点扩散是双向的,数列最终状态可以以此模拟,而花费的时间是(max{连续变动点长度} + 1) / 2。
/****************************************************
>Created Date: 2015-10-25-16.56.50
>My Soul, Your Beats!
****************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1 << 30;
const long long LINF = 1LL << 50;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const double PI = acos(-1.0);
const double eps = 1e-6;
int seq[500020], c[500020];
int res[500020];
int main(){
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &seq[i]);
res[1] = seq[1], res[n] = seq[n];
for(int i = 2; i < n; i++){
if(seq[i] == seq[i - 1] || seq[i] == seq[i + 1]) continue;
c[i] = 1;
}
int ans = 0, cnt = 0;
for(int i = 2; i <= n; i++){
if(c[i] == 0) {
ans = max(ans, cnt);
cnt = 0;
}
else cnt++;
}
int t = ans % 2;
ans /= 2;
ans += t;
int pre = 1;
for(int i = 2; i <= n; i++){
if(c[i] == 0){
for(int j = pre +1, k = i - 1; j <= k; j++, k--)
res[j] = seq[pre], res[k] = seq[i];
pre = i;
res[i] = seq[i];
}
}
printf("%d\n", ans);
for(int i = 1; i<= n; i++) printf("%d ", res[i]);
putchar('\n');
return 0;
}
/*
14
0 1 0 1 0 1 0 1 0 1 0 1 0 1
*/
D. Chip’n Dale Rescue Rangers
题意:一架飞船从起点飞到终点,途中会刮大风,风速和风向会在某一时间点变换,问飞船最快多久可以到达终点。
思路:开始没有读懂题意。。后来发现是个很水的物理题。。
假设起点相对风速为0,x秒后飞船被刮到点p,计算点p的飞船能否在x秒内无风飞到终点,二分时间即可。
/****************************************************
>Created Date: 2015-10-01-13.34.56
>My Soul, Your Beats!
****************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1 << 30;
const long long LINF = 1LL << 50;
const int M = 1e5 + 10;
const double PI = acos(-1.0);
const double eps = 1e-6;
int main(){
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
double x, y, dx, dy, ux, uy, wx, wy, t, v;
while(cin >> x >> y >> dx >> dy >> v >> t >> ux >> uy >> wx >> wy){
double left = 0.0, right = 100000000.0, cx, cy;
double distx = dx - x, disty = dy - y;
for(int i = 0; i < 200; i++){
double mid = (left + right) / 2;
if(mid > t){
cx = x + ux * t + wx * (mid - t);
cy = y + uy * t + wy * (mid - t);
}
else {
cx = x + ux * mid;
cy = y + uy * mid;
}
double dist = (dx - cx) * (dx - cx) + (dy - cy) * (dy - cy);
double dist2 = v * v * mid * mid;
if(dist2 > dist) right = mid;
else left = mid;
}
printf("%.11f\n", left);
}
return 0;
}
E. Three States
题意:一个二维图上有1、2、3三种联通块,现在要在空地修路,问联通三个块最少需要修多长的路。
思路:答案一定是1-2的距离、2-3的距离、1-3的距离中任选两个组合或者以某一空点为连接点连接块1、2、3,所以分别以三个块搜,搜出每点点到各块距离和块与块之间的最短距离,按上面的结论计算即可。
代码写的很挫,跑全是空地的图比较慢。
/****************************************************
>Created Date: 2015-10-25-16.56.50
>My Soul, Your Beats!
****************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 1 << 29;
const long long LINF = 1LL << 50;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const double PI = acos(-1.0);
const double eps = 1e-6;
int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
struct Node{
int x, y, steps;
bool operator < (const Node &i) const {
return steps > i.steps;
}
Node(){}
Node(int a, int b, int s):x(a), y(b), steps(s){}
};
int dist[4][4];
int vis[1010][1010][4], n, m;
void func(int x, int y){
for(int i = 1; i <= 3; i++)
for(int j = i + 1; j <= 3; j++)
if(vis[x][y][i] != -1 && vis[x][y][j] != -1){
dist[i][j] = dist[j][i] = min(dist[i][j], vis[x][y][i] + vis[x][y][j] - 1);
}
}
priority_queue<Node> pq;
char mp[1010][1010];
void bfs(int x, int y, int state){
Node node(x, y, 0);
vis[x][y][state] = 0;
pq.push(node);
while(!pq.empty()){
node = pq.top();
pq.pop();
for(int i = 0; i < 4; i++){
int x = node.x + d[i][0], y = node.y + d[i][1];
if(x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#') continue;
if(vis[x][y][state] != -1) continue;
if(mp[x][y] >= '1' && mp[x][y] <= '3' && mp[x][y] != state + '0'){
int a = mp[x][y] - '0', b = state;
dist[a][b] = dist[b][a] = min(dist[a][b], node.steps);
vis[x][y][state] = dist[a][b];
continue;
}
Node tmp = node;
tmp.x = x, tmp.y = y;
if(mp[x][y] == '.') tmp.steps++;
vis[x][y][state] = tmp.steps;
pq.push(tmp);
}
}
}
int main(){
// freopen("in.in", "r", stdin);
memset(vis, -1, sizeof vis);
for(int i = 1; i <= 3; i++)
for(int j = 1; j <= 3; j++)
dist[i][j] = INF;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++) scanf("%s", mp[i]);
int x1, y1, x2, y2, x3, y3;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(mp[i][j] == '1') x1 = i, y1 = j;
else if(mp[i][j] == '2') x2 = i, y2 = j;
else if(mp[i][j] == '3') x3 = i, y3 = j;
bfs(x1, y1, 1);
bfs(x2, y2, 2);
bfs(x3, y3, 3);
int ans = INF;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
if(mp[i][j] >= '1' && mp[i][j] <= '3') continue;
func(i, j);
if(vis[i][j][1] != -1 && vis[i][j][2] != -1 && vis[i][j][3] != -1)
ans = min(vis[i][j][1] + vis[i][j][2] + vis[i][j][3] - 2, ans);
}
ans = min(min(dist[1][2] + dist[2][3], ans), min(dist[1][2] + dist[1][3], dist[1][3] + dist[2][3]));
printf("%d\n", ans >= INF ? -1 : ans);
}