A. Beru-taxi
人站在(a,b),n辆车从(xi,yi)以速度vi向她开去,问最短到达时间。遍历一遍就好。
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e3 + 50;
const int MAXM = 250000 + 50;
int x, y, n;
int main()
{
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d%d", &x, &y))
{
double ans = 100000000;
scanf("%d", &n);
while (n--)
{
int a, b, v;
scanf("%d%d%d", &a, &b, &v);
ans = min(ans, sqrt((a - x) * (a - x) + (b - y) * (b - y)) / v);
}
printf("%.10lf\n", ans);
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}
B. Interesting drink
给n个数组成的序列,每次输入一个数,输出不大于输入的个数。
二分查找下标即可。
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 50;
const int MAXM = 250000 + 50;
int n, q, num[MAXN], coin[MAXN];
int main()
{
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
scanf("%d", &num[i]);
sort(num + 1, num + n + 1);
scanf("%d", &q);
while (q--)
{
int t;
scanf("%d", &t);
if (t < num[1])
{
printf("0\n");
continue;
}
int pos = lower_bound(num + 1, num + n + 1, t + 1) - num - 1;
printf("%d\n", pos);
}
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}
C. Hard problem
n个字符串,现在要通过翻转若干个字符串把这n个字符串变成字典序递增的,每个字符串翻转的代价为ci。求变为字典序花费的最小代价,或-1。
简单dp。
dp[i][0]表示第i个字符串没有翻转,同时前i个字符串是按字典序排列的最小代价。dp[i][1]则为第i个被翻转的最小代价。INF为不可能达到这个状态。
状态转移见代码。
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 50;
const int MAXM = 250000 + 50;
int n;
LL dp[MAXN][2], cost[MAXN];
string s[MAXN][2];
bool g(string a, string b)
{
if (a == b)
return true;
int len1 = a.size(), len2 = b.size();
for (int i = 0; i < min(len1, len2); i++)
if (a[i] != b[i])
return a[i] > b[i];
return len1 > len2;
}
int main()
{
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (cin >> n)
{
for (int i = 0; i < n; i++)
cin >> cost[i];
for (int i = 0; i < n; i++)
{
cin >> s[i][0];
s[i][1].assign(s[i][0].rbegin(), s[i][0].rend());
}
dp[0][0] = 0;
dp[0][1] = cost[0];
for (int i = 1; i < n; i++)
{
dp[i][0] = dp[i][1] = INFLL;
if (g(s[i][0], s[i - 1][1]))
dp[i][0] = min(dp[i][0], dp[i - 1][1]);
if (g(s[i][0], s[i - 1][0]))
dp[i][0] = min(dp[i][0], dp[i - 1][0]);
if (g(s[i][1], s[i - 1][1]))
dp[i][1] = min(dp[i][1], dp[i - 1][1] + cost[i]);
if (g(s[i][1], s[i - 1][0]))
dp[i][1] = min(dp[i][1], dp[i - 1][0] + cost[i]);
}
LL ans = min(dp[n - 1][0], dp[n - 1][1]);
printf("%I64d\n", ans == INFLL ? -1 : ans);
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}
D. Vasiliy's Multiset
存在一个multiset,然后q次查询,每次可以插入或者删除一个元素,或者查询multiset中与x的最大异或值。
非常经典的字典树题目。
建一棵31层的二叉字典树,从树根到叶子节点分别为数字的最高位到最低位,节点记录一下个数。查询的时候只需要贪心着走就可以求出最大异或值。
插入或删除对应把x插入字典树或者从字典树删除,查询就是上文所述的形式。
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 50;
const int MAXM = 250000 + 50;
int n;
struct node
{
int v;
node* next[2];
node()
{
v=0;
memset(next,NULL,sizeof(next));
}
}*root;
void trie_insert(node* start,int word, int c)
{
node* now=start;
for (int i = 30; i >= 0; i--)
{
int id = (word & (1 << i)) == 0 ? 0 : 1;
if(now->next[id]==NULL)
now->next[id]=new node();
now=now->next[id];
now->v += c;
}
}
int trie_query(node* start,int word)
{
int res = 0;
node* now=start;
for (int i = 30; i >= 0; i--)
{
int id = (word & (1 << i)) == 0 ? 0 : 1;
if (now->next[1 - id]!=NULL && now->next[1 - id]->v != 0)
{
res += (1 << i);
now = now->next[1 - id];
}
else if (now->next[id]!=NULL)
now = now->next[id];
}
return res;
}
int main()
{
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d", &n))
{
char op[5];
int x;
node* root = new node();
trie_insert(root, 0, 1);
while (n--)
{
scanf("%s%d", op, &x);
if (op[0] == '+')
trie_insert(root, x, 1);
else if (op[0] == '-')
trie_insert(root, x, -1);
else if (op[0] == '?')
printf("%d\n", trie_query(root, x));
}
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}
E. Working routine
题目给出一个矩阵,每次把两个大小相等、不相交、不重叠的子矩阵交换位置,最后输出矩阵。矩阵为1000*1000,q为10000。
模拟十字链表。
考虑到1000*10000不大,可以做到每次交换暴力循环一遍。就可以把矩阵放入一个十字链表中,就是具有right指针和down指针的链表。
然后,每次交换,我们只需要把两个矩阵的边界节点与外界的指针完全交换,就可以完成子矩阵的交换。
每次通过横纵坐标,从起点开始枚举,直到到达需要的节点,然后就是枚举并交换指针了。
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int, int> PII;
#define mp make_pair
#define pb push_back
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
#define lowbit(x) ((x)&(-(x)))
#define bitcnt(x) __builtin_popcount(x)
#define bitcntll(x) __builtin_popcountll(x)
#define debug puts("-------------");
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e3 + 50;
const int MAXM = 4e5 + 50;
int n, m, q;
struct node{
node *right, *down;
int val;
}G[MAXN][MAXN];
node *Find(node *s, int x, int y) {
while (x--) s = s->down;
while (y--) s = s->right;
return s;
}
int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d%d%d", &n, &m, &q)) {
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++) {
G[i][j].right = &G[i][j + 1];
G[i][j].down = &G[i + 1][j];
if (i && j) scanf("%d", &G[i][j].val);
}
node *s = &G[0][0];
while (q--) {
int a, b, c, d, h, w;
scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &h, &w);
node *l1 = Find(s, a, b - 1), *r1 = Find(s, a, b - 1 + w), *u1 = Find(s, a - 1, b), *d1 = Find(s, a - 1 + h, b);
node *l2 = Find(s, c, d - 1), *r2 = Find(s, c, d - 1 + w), *u2 = Find(s, c - 1, d), *d2 = Find(s, c - 1 + h, d);
while (w--) {
swap(u1->down, u2->down);
swap(d1->down, d2->down);
u1 = u1->right, u2 = u2->right, d1 = d1->right, d2 = d2->right;
}
while (h--) {
swap(l1->right, l2->right);
swap(r1->right, r2->right);
l1 = l1->down, l2 = l2->down, r1 = r1->down, r2 = r2->down;
}
}
node *x;
for (int i = 1; i <= n; i++) {
x = G[i][0].right;
for (int j = 1; j <= m; j++) {
printf("%d%c", x->val, " \n"[j == m]);
x = x->right;
}
}
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}