1. 程式人生 > >2016.4.23 浙江省賽題解

2016.4.23 浙江省賽題解

Apples and Ideas
Time Limit: 2 Seconds      Memory Limit: 65536 KB

"If you have an apple and I have an apple and we exchange these apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange these ideas, then each of us will have two ideas." - George Bernard Shaw

Now Alice has A apples and B ideas, while Bob has C apples and D ideas, what will they have if they exchange all things?

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The only line contains four integers A

BCD (0 <= ABCD <= 100) - as the problem described.

Output

For each test case, output two lines. First line contains two integers, indicating the number of Alice's apples and ideas; second line contains two integers, indicating the number of Bob's apples and ideas.

Sample Input

4
0 0 5 30
20 25 20 0
20 25 20 15
20 25 25 30

Sample Output

5 30
0 30
20 25
20 25
20 40
20 40
25 55
20 55

A. APPLES AND IDEAS

A,C=C,A; B=D=B+D;

#include <bits/stdc++.h>
using namespace std;

int main() {
  int T; cin >> T;
  for (int _ = 0; _ < T; ++_ ) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << c << " " << b + d << endl;
    cout << a << " " << b + d << endl;
  }
  return 0;
}


More Health Points
Time Limit: 2 Seconds      Memory Limit: 65536 KB

Recently, Bright Luna is playing a massively multiplayer online role-playing game (MMORPG) called Legendary Age. Designers of this game prepared many mini-games during daily operations. Players can ignore these games without any loss. If you spend some time in them, you can always obtain some bonuses. This is one of the fascinating features ofLegendary Age.

One day, Bright Luna had a fight with other players. After that he came back to the city to recover his Health Points (HP). Surprisingly, the NPC gave him a special choice. The NPC can teleport Bright Luna to a colorful fantasy world with N dungeons. These dungeons are identified from 1 through N. Each dungeon has exactly a one-way exit leading to another dungeon which has a smaller identifier except for the dungeon 1, which has no exit.

There is a buff or debuff in each dungeon which can be described by an integer Bi. Bright Luna will get the i-th buff/debuff when he enters the i-th dungeon. When he leaves a dungeon or leaves the world, the buffs/debuffs he has obtained will change his HP by Bi. For example, If he already visited the dungeons 6 -> 4 -> 3, when he leaves the dungeon 3, his HP will be changed by (B6 + B4 + B3). The buffs/debuffs will not expire until he completes the adventure.

The NPC had shown the world map to Bright Luna. Bright Luna can choose the dungeon he enters at the beginning and he can leave the world at any dungeon. Of course, he can refuse to take this adventure. Can you help Bright Luna to gain as many health points as possible?

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 ≤ N ≤ 105).

The second line contains N integers Bi (|Bi| ≤ 108).

The third line contains N - 1 integers Fi (2 ≤ i ≤ NFi < i) indicating the dungeons which the exit of dungeon i lead to.

Output

For each case, please output the maximum health points Bright Luna can gain.

Sample Input

4
4
1 4 3 2
1 1 1
4
1 2 -3 4
1 2 3
4
-4 2 -3 4
1 2 3
3
-1 -2 -3
1 1

Sample Output

9
12
8
0

Hint

In the first example, we can start from dungeon 2 and end at dungeon 1.

In the second example, we can start from dungeon 4 and end at dungeon 1.

In the third example, we can start from dungeon 4 and end at dungeon 2.

B. MORE HEALTH POINTS

考慮鏈上的版本(其實就是Bear and Bowling 4),給出nn個數a_1,a_2,...,a_na1,a2,...,an,求出\sum\limits_{i=l}^{r}{(i-l+1)a_i}i=lr(il+1)ai的最大值。令s_i=\sum\limits_{j=1}^{i}a_j, p_i=\sum\limits_{j=1}^{i}ja_jsi=j=1iaj,pi=j=1ijaj,那麼就是求p_r-p_{l-1}-l(s_r-s_{l-1})prpl1l(srsl1)的最大值。如果固定rr,把式子變換一下就是找到一個ll最大化p_r-ls_r-p_{l-1}+ls_{l-1}prlsrpl1+lsl1,維護一個凸殼就可以搞了。推廣到樹上把鏈上用到的單調佇列可持久一下就好了,複雜度O(n\log n)O(nlogn)

也可以樹分治。考慮每次分治中心gg,一條合法的在原來有根樹上的路徑是這麼組成的:gg往下標號一路遞減的路徑+gg往下標號一路遞增的路徑。類似地,推一下合併需要的式子,會發現也是可以用凸殼維護的,複雜度O(n\log^2n)O(nlog2n)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

class Solution {
  static const int MAXN = 100000 + 10;
  static const LL inf = 1ll << 60;
  vector<int> G[MAXN];
  int w[MAXN], n;

  struct Line {// m * x + b
    LL m, b;
    double inter(const Line &r) const {
      return (r.b - b) / (m - r.m);
    }
    inline LL eval(LL x) {return m * x + b;}
  } Q[MAXN];

  int rt, mins, total, top;
  int vs[MAXN], sz[MAXN], dep[MAXN];
  LL val[MAXN], ps[MAXN], ret;

  void getCenter(int u, int f = -1) {
    int mx = 0; sz[u] = 1;
    for (auto &v: G[u]) if (v != f && !vs[v]) {
      getCenter(v, u); sz[u] += sz[v];
      mx = max(mx, sz[v]);
    }
    mx = max(mx, total - sz[u]);
    if (mx < mins) mins = mx, rt = u;
  }
  int dfs1(int u, int d = 1) {
    ret = max(ret, val[d]);
    for (auto &v: G[u]) if (!vs[v] && u > v) {
      ps[v] = ps[u] + w[v];
      val[d + 1] = val[d] + ps[v];
      return dfs1(v, d + 1) + 1;
    }
    return 1;
  }
  void dfs2(int u, int d = 0, LL sum = 0) {
    sum += 1ll * d * w[u];
    // max(ps[u] * m + b + sum)
    int left = 0, right = top - 2;
    while (left < right) {
      int mid = (left + right) >> 1;
      if (Q[mid].eval(ps[u]) >= Q[mid + 1].eval(ps[u])) right = mid;
      else left = mid + 1;
    }
    ret = max(ret, Q[left].eval(ps[u]) + sum);
    if (left + 1 < top) ret = max(ret, Q[left + 1].eval(ps[u]) + sum);
    for (auto &v: G[u]) if (!vs[v] && u < v) {
      ps[v] = ps[u] + w[v];
      dfs2(v, d + 1, sum);
    }
  }
  void solve(int u, int _tot) {
    total = _tot; mins = _tot * 2;
    getCenter(u); u = rt; vs[u] = 1; getCenter(u);
    val[1] = ps[u] = w[u];
    int md = dfs1(u);
    top = 0;
    for (int i = 1; i <= md; ++i) {
      Line now = (Line){i, val[i]};
      while (top >= 2 && Q[top - 2].inter(Q[top - 1]) >= Q[top - 1].inter(now)) --top;
      Q[top++] = now;
    }
    ps[u] = 0; dfs2(u);
    for (int i = 0; i <= md; ++i) val[i] = -inf;
    for (auto &v: G[u]) if (!vs[v]) {
      solve(v, sz[v]);
    }
  }

public:
  void run() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
      scanf("%d", w + i); G[i].clear();
    }
    for (int i = 1; i < n; ++i) {
      int x; scanf("%d", &x); --x;
      G[x].push_back(i);
      G[i].push_back(x);
    }
    ret = 0;
    for (int i = 0; i < n; ++i) val[i] = -inf;
    memset(vs, 0, sizeof(vs[0]) * n);
    solve(0, n);
    printf("%lld\n", ret);
  }
} sol;

int main() {
  int T; scanf("%d", &T);
  for (int cas = 1; cas <= T; ++cas) sol.run();
  return 0;
}


Defuse the Bomb
Time Limit: 2 Seconds      Memory Limit: 65536 KB

The bomb is about to explode! Please defuse it as soon as possible!

There is a display showing a number from 1 to 4 on the bomb. Besides this, there are 4 buttons under the display. Each button is labeled by a number from 1 to 4. The numbers on the buttons are always distinct.

There are 5 defusing stages in total. Pressing the correct button can progress the bomb to the next defusing stage. The number on the display and the number on each button may be different in different stages. The bomb will be defused only when all 5 defusing stages get passed. Pressing the incorrect button will cause the bomb to explode immediately. Be careful!

Here is the detailed bomb defusing manual. Button positions are ordered from left to right.

Stage 1:

  • If the display is 1, press the button in the second position.
  • If the display is 2, press the button in the second position.
  • If the display is 3, press the button in the third position.
  • If the display is 4, press the button in the fourth position.

Stage 2:

  • If the display is 1, press the button labeled "4".
  • If the display is 2, press the button in the same position as you pressed in stage 1.
  • If the display is 3, press the button in the first position.
  • If the display is 4, press the button in the same position as you pressed in stage 1.

Stage 3:

  • If the display is 1, press the button with the same label you pressed in stage 2.
  • If the display is 2, press the button with the same label you pressed in stage 1.
  • If the display is 3, press the button in the third position.
  • If the display is 4, press the button labeled "4".

Stage 4:

  • If the display is 1, press the button in the same position as you pressed in stage 1.
  • If the display is 2, press the button in the first position.
  • If the display is 3, press the button in the same position as you pressed in stage 2.
  • If the display is 4, press the button in the same position as you pressed in stage 2.

Stage 5:

  • If the display is 1, press the button with the same label you pressed in stage 1.
  • If the display is 2, press the button with the same label you pressed in stage 2.
  • If the display is 3, press the button with the same label you pressed in stage 4.
  • If the display is 4, press the button with the same label you pressed in stage 3.

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:

There are 5 lines. Each line contains 5 integers DB1B2B3B4 indicating the number on the display and the numbers on the buttons respectively. The i-th line correspond to the i-th stage.

Output

For each test case, output 5 lines. The i-th line contains two integers indicating the position and the label of the correct button for the i-th stage.

Sample Input

1
4 2 1 3 4
2 2 4 3 1
4 3 1 4 2
4 3 4 2 1
2 3 1 2 4

Sample Output

4 4
4 1
3 4
4 1
2 1

Hint

Keep talking with your teammates and nobody explodes!

C. DEFUSE THE BOMB

按照題目意思模擬。

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;

vector<PII> res;
int b[5][5], a[5];

int main() {
  int T; scanf("%d", &T);
  for (int cas = 1; cas <= T; ++cas) {
    int r[5];
    for (int i = 0; i < 5; ++i) {
      for (int j = 0; j < 5; ++j) {
        cin >> b[i][j];
      }
    }
    // stage 1
    int *x = b[0], d = x[0];
    if (d <= 2) r[0] = 2;
    else r[0] = d;
    // stage 2
    x = b[1], d = x[0];
    for (int i = 1; i < 5; ++i) a[x[i]] = i;
    if (d == 1) r[1] = a[4];
    else if (d == 3) r[1] = 1;
    else r[1] = r[0];
    // stage 3
    x = b[2], d = x[0];
    for (int i = 1; i < 5; ++i) a[x[i]] = i;
    if (d == 1) r[2] = a[b[1][r[1]]];
    else if (d == 2) r[2] = a[b[0][r[0]]];
    else if (d == 4) r[2] = a[4];
    else r[2] = d;
    // stage 4
    x = b[3], d = x[0];
    if (d == 1) r[3] = r[0];
    else if (d == 2) r[3] = 1;
    else r[3] = r[1];
    // stage 5
    x = b[4], d = x[0];
    for (int i = 1; i < 5; ++i) a[x[i]] = i;
    if (d <= 2) r[4] = a[b[d - 1][r[d - 1]]];
    else r[4] = a[b[6 - d][r[6 - d]]];
    for (int i = 0; i < 5; ++i) {
      cout << r[i] << " " << b[i][r[i]] << endl;
    }
  }
  return 0;
}


The Lucky Week
Time Limit: 2 Seconds      Memory Limit: 65536 KB

Edward, the headmaster of the Marjar University, is very busy every day and always forgets the date.

There was one day Edward suddenly found that if Monday was the 1st, 11th or 21st day of that month, he could remember the date clearly in that week. Therefore, he called such week "The Lucky Week".

But now Edward only remembers the date of his first Lucky Week because of the age-related memory loss, and he wants to know the date of the N-th Lucky Week. Can you help him?

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:

The only line contains four integers YMD and N (1 ≤ N ≤ 109) indicating the date (Y: year, M: month, D: day) of the Monday of the first Lucky Week and the Edward's query N.

The Monday of the first Lucky Week is between 1st Jan, 1753 and 31st Dec, 9999 (inclusive).

Output

For each case, print the date of the Monday of the N-th Lucky Week.

Sample Input

2
2016 4 11 2
2016 1 11 10

Sample Output

2016 7 11
2017 9 11

D. THE LUCKY WEEK

可以觀察到1753年1月1日是週一,然後我們知道大概再過2800年就會重複。於是預處理出1753.01.01後面2800年的所有lucky week。之後根據迴圈節算一下就好了。

#include <bits/stdc++.h>
using namespace std;

vector<tuple<int, int, int>> p;

int day(int y, int m, int d) {
  int tm = m >= 3 ? (m - 2) : (m + 10);
  int ty = m >= 3 ? y : (y - 1);
  return (ty + ty / 4 - ty / 100 + ty / 400 + (int)(2.6 * tm - 0.2) + d) % 7;
}

void run() {
  int y, m, d, n; cin >> y >> m >> d >> n;
  y -= 1753; n += y / 2800 * p.size();
  y %= 2800;
  n += lower_bound(p.begin(), p.end(), make_tuple(y, m, d)) - p.begin() - 1;
  auto res = p[n % p.size()];
  y = get<0>(res);
  m = get<1>(res);
  d = get<2>(res);
  y += n / p.size() * 2800 + 1753;
  cout << y << " " << m << " " << d << endl;
}

int main() {
  cerr << day(1753, 1, 1) << endl;
  for (int y = 0; y < 2800; ++y) {
    for (int m = 1; m <= 12; ++m) {
      if (day(y + 1753, m,  1) == 1) p.push_back(make_tuple(y, m,  1));
      if (day(y + 1753, m, 11) == 1) p.push_back(make_tuple(y, m, 11));
      if (day(y + 1753, m, 21) == 1) p.push_back(make_tuple(y, m, 21));
    }
  }
  int T; scanf("%d", &T);
  for (int cas = 1; cas <= T; ++cas) run();
  return 0;
}



Modulo Query
Time Limit: 2 Seconds      Memory Limit: 65536 KB

One day, Peter came across a function which looks like:

  • F(1, X) = X mod A1.
  • F(iX) = F(i - 1, X) mod Ai, 2 ≤ i ≤ N.
Where A is an integer array of length NX is a non-negative integer no greater than M.

Peter wants to know the number of solutions for equation F(NX) = Y, where Y is a given number.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers N and M (2 ≤ N ≤ 105, 0 ≤ M ≤ 109).

The second line contains N integers: A1A2, ..., AN (1 ≤ Ai ≤ 109).

The third line contains an integer Q (1 ≤ Q ≤ 105) - the number of queries. Each of the following Q lines contains an integer Yi (0 ≤ Yi ≤ 109), which means Peter wants to know the number of solutions for equation F(NX) = Yi.

Output

For each test cases, output an integer S = (1 ⋅ Z1 + 2 ⋅ Z2 + ... + Q ⋅ ZQ) mod (109 + 7), where Zi is the answer for the i-th query.

Sample Input

1
3 5
3 2 4
5
0
1
2
3
4

Sample Output

8

Hint

The answer for each query is: 4, 2, 0, 0, 0.

E. MODULO QUERY

可以觀察到一個區間對一個數取模後的結果也可以用區間來表示,並且這些區間的左端點都是0。於是直接用一個map存每次取模之後的區間和這個區間出現次數,要取模的時候,找出所有右端點大於等於當前模數的所有區間,暴力算一下結果即可。對於查詢,二分下位置,求個字尾和就好了。下面分析下複雜度為什麼是對的。

眾所周知:一個數對一堆數取模,最多會有\loglog次值的改變。對於這題每對一個新數取模,最多隻會增加一個區間。考慮當前區間是[0,v_i][0,vi],要對xx取模,那麼對於v_i < xvi<x的區間沒有變化,對於v_i \ge xv

相關推薦

2016.4.23 浙江省題解

Apples and Ideas Time Limit: 2 Seconds      Memory Limit: 65536 KB "If you have an apple and I have an apple and we exchange t

2016 ACM青島區域題解

A 水題,直接暴力模擬即可。 #include <bits/stdc++.h> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=40000; typede

10-4國慶節第七場模擬題解

一次 .com spa space using ans -m 增加 直接 10-4 國慶節第七場模擬賽題解 T1工廠 (factory) 水 #include<iostream> #include<cstdio> #define int long l

2016 icpc北京區域重現---題解

題意:  Weiming Lake, also named “Un-named Lake”, is the most famous scenic spot in Peking University. It is located in the north of the c

The 2016 ACM-ICPC Asia China-Final (Shanghai) Contest---重現題解

題意: Mr. Panda is one of the top specialists on number theory all over the world. Now Mr. Panda is investigating the property of the pow

2016 acm/icpc 青島網路 題解(hdu 5878-5889,9道題)

5878.I Count Two Three(打表預處理,二分) 題目大意:有一些數可以寫成2a3b5c7d的形式,稱之為”I count two three numbers”.輸入一個數,問比他大的最小的”I count two three number

2016第七屆藍橋杯C/C++ B組省題解 H題

四平方和 四平方和定理,又稱為拉格朗日定理: 每個正整數都可以表示為至多4個正整數的平方和。 如果把0包括進去,就正好可以表示為4個數的平方和。 比如: 5 = 0^2 + 0^2 + 1^2 + 2

2016第七屆藍橋杯C/C++ B組省題解

前言: 已經是第二次參加藍翔杯了,又是凌晨四點半天還沒亮就要屁顛屁顛的起來。然後坐著學校大巴車來到成都理工。到了學校提前了2個小時啊。。。瞌睡來忙了沒地方睡。和一幫兄弟帶著三個大一的繞著理工逛了一圈,說實話,學校風景真不乍地。回到考場基本就要到比賽時間了。好了不多說了開始

2016.10.05【初中部 NOIP普及組 】模擬題解

這次比賽又翻車了....... T1 這道題一開始以為是一道大水題,格外簡單:把分子乘起來,把分母也乘起來,然後約分。但看到資料後,發現這樣根本不行:一千個數,最大不超過一千(就是999),最大的積就

2016中北大學ACM程式設計新生題解

新生賽題目地址 a or an 輸入字串後判斷第一個字元是不是’a’,’e’,’i’,’o’,’u’,即可。 #include<algorithm> #include <iostream> #include <cstri

2016移動開發校之Android實踐

android 學校組織的《移動互聯網應用軟件開發》技能競賽,所有參賽命題均基於 Android 4.2 版本實現,比賽推薦使用的開發環境為:ADT, JDK 1.6, Android SDK 4.1   經過了四天的培訓我們小組選擇在計算器應用 計算器能夠實現: 1. 數學四則運算 2. 實現開方元算

三、環境搭建 之 Windows10 安裝 Apache2.4.23

地址 管理員 解壓 技術 項目 可能 配置 httpd 安裝步驟 操作系統:Windows10 家庭中文版 Apache版本:2.4.23 下載地址:http://pan.baidu.com/s/1nuB6cjf 參考文檔:win7(64位)php5.5-Apache2.4

Linux之安裝apache2.4.23

duyuheng linux apache2.4.23Linux之安裝apache2.4.23安裝環境:操作系統:Centos7.2,關閉selinux新版本的httpd-2.4新增以下特性;新增模塊;mod_proxy_fcgi(可提供fcgi代理)mod_ratelimit(限制用戶帶寬)mod_requ

新概念4-23

end gre sse any tro ise pac keep virt No two sorts of birds practise quite the same sort of flight; the varieties are infinite; but two

2016北京集訓測試】azelso(unfinished)

open 就會 技術分享 sca esp span pan 套路 for [吐槽]   首先當然是要orzyww啦   以及orzyxq奇妙順推很強qwq   嗯。。怎麽說呢雖然說之前零零散散做了一些概d的題目但是總感覺好像並沒有弄得比較明白啊。。(我的媽果然蒟蒻

NK分治題解

右上角 long 轉化 while brush ges pre 最小 amp A-航運調度: 一道比較裸的三分題,令$F(t)$為$t$時刻船兩兩之間距離的最大值,直接三分求$F(t)$在$[0,+\infty)$的最小值即可。 時間復雜度:$O(n^2 log n)$

【模擬題解錦集】——Hzoi

質數 spa else align can 11.2 == 問題 輔助 Day IXV:   題目鏈接   T1:    對於二者之一$n$,每次選取要麽變成$2n$,要麽是$n-(tot-n)=2*n-tot$,即最後結果為$n*2^{k}(\mod tot)

10.23模擬

image ans img front 整數 最短路 strong font 次數 T1叉叉 題目描述 現在有一個字符串,每個字母出現的次數均為偶數。接下來我們把第一次出現的字母a和第二次出現的a連一條線,第三次出現的和四次出現的字母a連一條線,第五次出現的和六次出現的字母

CQUPT萌新題解(附F代碼)

mil 不能 遞推 strcmp 記憶化搜索 代碼 好用 字典 std 題解或許會遲到,但永遠不會缺席(逃 還是感謝qs手下留情和mzjj給弱智一點面子,才讓本蒟蒻拿到了鍵盤( 題目貼了網址也沒意義,鶸校只讓內網進(蘭兒鶸校oj也見不得人 那麽開始正題: T1,T2: OR

HDU 5880 Family View (2016 青島網絡 C題,AC自動機)

題意 post 更新 log 青島 bsp problem pid target 題目鏈接 2016 青島網絡賽 Problem C 題意 給出一些敏感詞,和一篇文章。現在要屏蔽這篇文章中所有出現過的敏感詞,屏蔽掉的用$‘*‘$表示。 建立$AC$自動機,查