首页 > 代码库 > DUT Star Round2

DUT Star Round2

A.Zeratu的军训游戏

Problems: 开灯问题,问无数次操作之后第n盏灯的状态

Analysis:

  cj:平方数有奇数个约数

Tags: Implementation

 

B.Zeratud的完美区间

Problems: 对于一个[1, n]的排列中,询问区间[l, r]中数字是否连续

Analysis:

   cj:智障的我想了一个及其错误的算法,后来被lucky_ji点醒,if (区间最大值最小值之差 == 区间长度)就好了。。

Tags: Data Structure, Dynamic Programming

 

C. ACM群日常禁言一万年

Problems: 秒数转换为xdays, hh/mm/ss

Tags: Implementation

 

D.Zeratud与LCM

Problems: 构造长度为n的序列{a},使得LCM(a1, a2, ......, an) = a1 + a2 + ...... + an

Analysis:

Tags: Math

 

E.小q与面试题

Problems: 维护一个可以查询最小值的栈

Analysis:

  cj: 这种题最适合用STL乱搞了

Tags: Data Structure

技术分享
 1 #define PRON "e"
 2 #include <set>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <iostream>
 7 #include <algorithm>
 8 #define inf 0x3f3f3f3f
 9 using namespace std;
10 typedef long long ll;
11 
12 const int maxn = 500000 + 10;
13 
14 stack<int> q;
15 multiset<int> s;
16 
17 char ch;
18 inline void read(int & x){
19     int flag = 1;
20     do {
21         if (ch == -)
22             flag = -1;
23         ch = getchar();
24     } while (!(0 <= ch && ch <= 9));
25     
26     x = 0;
27     do {
28         x = x * 10 + ch - 0;
29         ch = getchar();
30     } while (0 <= ch && ch <= 9);
31 
32     x *= flag;
33 }
34 
35 int main(){
36 #ifndef ONLINE_JUDGE
37     freopen(PRON ".in", "r", stdin);
38 #endif
39     s.clear();
40     while (not q.empty())
41         q.pop();
42 
43 
44     int T, a, b, sum = 0;
45     read(T);
46     while (T --){
47         read(a);
48         if (a == 0){
49             read(b);
50             ++ sum;
51             s.insert(b);
52             q.push(b);
53         }
54         if (a == 1){
55             if (sum == 0)
56                 puts("ERROR!");
57             else
58                 printf("%d\n", q.top());
59         }
60         if (a == 2){
61             if (sum == 0)
62                 puts("ERROR!");
63             else
64                 printf("%d\n", *s.begin());
65         }
66         if (a == 3){
67             if (sum == 0)
68                 puts("ERROR!");
69             else {
70                 -- sum;
71                 s.erase(s.find(q.top()));
72                 q.pop();
73             }
74         }
75     }
76 }
77         
78     
Code by cj

 

F.Zeratu与QQ堂

Problems: 放置m次炸弹(不能在转快上放置),每次炸掉上下左右最近的砖块,输出最后剩余的砖块

Analysis:

  cj: 直接模拟会Time Limit Exceeded,用row[i]存储第i行的砖块的j坐标,col[j]存储第j行砖块的i坐标。每一次放置炸弹成功后,二分找到前后和左右的砖块并且erase

Tags: Implementation

技术分享
  1 #define PRON "f"
  2 #include <vector>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <iostream>
  6 #include <algorithm>
  7 using namespace std;
  8 typedef long long ll;
  9 
 10 #define lb lower_bound
 11 #define r(y, x) lb(row[y].begin(), row[y].end(), x)
 12 #define c(x, y)    lb(col[x].begin(), col[x].end(), y)
 13 
 14 const int maxn = 1000 + 10;
 15 
 16 int n, sum = 0;
 17 char s[maxn][maxn];
 18 vector<int> row[maxn], col[maxn];
 19 
 20 int main(){
 21 #ifndef ONLINE_JUDGE
 22     freopen(PRON ".in", "r", stdin);
 23 #endif
 24 
 25     scanf("%d", &n);
 26     for (int i = 0; i < n; i ++){
 27         scanf("%s", s[i]);
 28 
 29         for (int j = 0; j < n; j ++)
 30             if (s[i][j] == *)
 31                 ++ sum;
 32     }
 33 
 34     for (int i = 0; i < n; i ++)
 35         for (int j = 0; j < n; j ++)
 36             if (s[i][j] == *)
 37                 row[i].push_back(j);
 38 
 39     for (int j = 0; j < n; j ++)
 40         for (int i = 0; i < n; i ++)
 41             if (s[i][j] == *)
 42                 col[j].push_back(i);
 43 
 44     int q, x, y, pos;
 45     scanf("%d", &q);
 46     while (q --){    
 47         scanf("%d %d", &y, &x);
 48 
 49         if ((not row[y].empty() && *(r(y, x)) == x) && (not col[x].empty() && *(c(x, y)) == y))
 50             continue;
 51 
 52         if (not row[y].empty()){
 53             -- sum;
 54             if (x < *row[y].begin()){
 55                 col[*row[y].begin()].erase(c(*row[y].begin(), y));
 56                 row[y].erase(row[y].begin());
 57             }
 58             else if (x > *(row[y].end() - 1)){
 59                 col[*(row[y].end() - 1)].erase(c(*(row[y].end() - 1), y));
 60                 row[y].erase(row[y].end() - 1);
 61             }
 62             else {
 63                 -- sum;
 64                 pos = r(y, x) - row[y].begin();
 65                 col[row[y][pos]].erase(c(row[y][pos], y));
 66                 col[row[y][pos - 1]].erase(c(row[y][pos - 1], y));
 67 
 68                 row[y].erase(pos + row[y].begin());
 69                 row[y].erase(pos - 1 + row[y].begin());
 70             }
 71         }
 72 
 73         if (not col[x].empty()){
 74             -- sum;
 75             if (y < *col[x].begin()){
 76                 row[*col[x].begin()].erase(r(*col[x].begin(), x));
 77                 col[x].erase(col[x].begin());
 78             }
 79             else if (y > *(col[x].end() - 1)){
 80                 row[*(col[x].end() - 1)].erase(r(*(col[x].end() - 1), x));
 81                 col[x].erase(col[x].end() - 1);
 82             }
 83             else {
 84                 -- sum;
 85                 pos = c(x, y) - col[x].begin();
 86                 row[col[x][pos]].erase(r(col[x][pos], x));
 87                 row[col[x][pos - 1]].erase(r(col[x][pos - 1], x));
 88 
 89                 col[x].erase(pos + col[x].begin());
 90                 col[x].erase(pos - 1 + col[x].begin());
 91             }
 92         }
 93 
 94     }
 95 
 96 
 97     printf("%d\n", sum);
 98 }
 99         
100     
Code by cj

 

G.Zeratu的最优路径

Problems: 数字“正方形”

Tags: Dynamic Programming

DUT Star Round2