bupt summer training for 16 #3 ——構造

https://vjudge.net/contest/172464

后來補題發現這場做的可真他媽傻逼

?

A.簽到傻逼題,自己分情況

 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using std::vector;
 6 using std::sort;
 7 
 8 typedef long long ll;
 9 
10 int n, m;
11 
12 ll a[2100], b[2100];
13 
14 ll sa, sb, dis, tmp, qaq;
15 
16 int t1 = -1, t2 = -1;
17 
18 int a1, a2, a3, a4;
19 
20 struct node {
21     ll x, y, z;
22     bool operator < (const node &a) const {
23         return x < a.x;
24     }
25 };
26 
27 vector <node> e;
28 
29 ll abs(ll x) {
30     return x < 0 ? -x : x;
31 }
32 
33 node find1(ll x) {
34     node ret = {0, -1, -1};
35     int l = 0, r = e.size() - 1, mid;
36     while(l <= r) {
37         int mid = (l + r) >> 1;
38         if(e[mid].x * 2 <= x) ret = e[mid], l = mid + 1;
39         else r = mid - 1;
40     }
41     return ret;
42 }
43 
44 node find2(ll x) {
45     node ret = {0, -1, -1};
46     int l = 0, r = e.size() - 1, mid;
47     while(l <= r) {
48         int mid = (l + r) >> 1;
49         if(e[mid].x * 2 > x) ret = e[mid], r = mid - 1;
50         else l = mid + 1;
51     }
52     return ret;
53 }
54 
55 int main() {
56     scanf("%d", &n);
57     for(int i = 1;i <= n;i ++)
58         scanf("%lld", &a[i]), sa += a[i];
59     scanf("%d", &m);
60     for(int i = 1;i <= m;i ++)
61         scanf("%lld", &b[i]), sb += b[i];
62     if(abs(sa - sb) <= 1) {
63         printf("%lld\n0\n", abs(sa - sb));
64         return 0;
65     }
66     qaq = tmp = dis = sa - sb;
67     for(int i = 1;i <= n;i ++) 
68         for(int j = 1;j <= m;j ++)
69             if(abs(qaq - (a[i] - b[j]) * 2) < abs(dis))
70                 dis = qaq - (a[i] - b[j]) * 2, t1 = i, t2 = j;
71     for(int i = 1;i <= n;i ++)
72         for(int j = i + 1;j <= n;j ++)
73             e.push_back((node){a[i] + a[j], i, j});
74     sort(e.begin(), e.end());
75     for(int i = 1;i <= m;i ++)
76         for(int j = i + 1;j <= m;j ++) {
77             ll k = b[i] + b[j];
78             node tt = find1(qaq + k * 2);
79             if(tt.y != -1 && abs(qaq + k * 2 - tt.x * 2) < abs(tmp)) tmp = qaq + k * 2 - tt.x * 2, a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
80             tt = find2(qaq + k * 2);
81             if(tt.y != -1 && abs(qaq + k *  2 - tt.x * 2) < abs(tmp)) tmp = qaq + k * 2 - tt.x * 2, a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
82         }
83     if(abs(qaq) <= abs(dis) && abs(qaq) <= abs(tmp)) printf("%lld\n0\n", abs(qaq));
84     else if(abs(dis) <= abs(tmp)) printf("%lld\n1\n%d %d", abs(dis), t1, t2);
85     else printf("%lld\n2\n%d %d\n%d %d", abs(tmp), a1, a2, a3, a4);
86     return 0;
87  }
View Code

想寫if-else結果寫完if忘記else了

?

B.我是暴力的O(n^2logn)

首先如果只交換一個數,那O(n^2)都會算吧

那交換兩個數呢,我把n個數兩兩結合并求和,再對他們排序

再枚舉另一數組的數對,二分一下嘗試更新答案

 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using std::vector;
 6 using std::sort;
 7 
 8 typedef long long ll;
 9 
10 int n, m;
11 
12 ll a[2100], b[2100];
13 
14 ll sa, sb, dis, tmp, qaq;
15 
16 int t1 = -1, t2 = -1;
17 
18 int a1, a2, a3, a4;
19 
20 struct node {
21     ll x, y, z;
22     bool operator < (const node &a) const {
23         return x < a.x;
24     }
25 };
26 
27 vector <node> e;
28 
29 ll abs(ll x) {
30     return x < 0 ? -x : x;
31 }
32 
33 node find1(ll x) {
34     node ret = {0, -1, -1};
35     int l = 0, r = e.size() - 1, mid;
36     while(l <= r) {
37         int mid = (l + r) >> 1;
38         if(e[mid].x * 2 <= x) ret = e[mid], l = mid + 1;
39         else r = mid - 1;
40     }
41     return ret;
42 }
43 
44 node find2(ll x) {
45     node ret = {0, -1, -1};
46     int l = 0, r = e.size() - 1, mid;
47     while(l <= r) {
48         int mid = (l + r) >> 1;
49         if(e[mid].x * 2 > x) ret = e[mid], r = mid - 1;
50         else l = mid + 1;
51     }
52     return ret;
53 }
54 
55 int main() {
56     scanf("%d", &n);
57     for(int i = 1;i <= n;i ++)
58         scanf("%lld", &a[i]), sa += a[i];
59     scanf("%d", &m);
60     for(int i = 1;i <= m;i ++)
61         scanf("%lld", &b[i]), sb += b[i];
62     if(abs(sa - sb) <= 1) {
63         printf("%lld\n0\n", abs(sa - sb));
64         return 0;
65     }
66     qaq = tmp = dis = sa - sb;
67     for(int i = 1;i <= n;i ++) 
68         for(int j = 1;j <= m;j ++)
69             if(abs(qaq - (a[i] - b[j]) * 2) < abs(dis))
70                 dis = qaq - (a[i] - b[j]) * 2, t1 = i, t2 = j;
71     for(int i = 1;i <= n;i ++)
72         for(int j = i + 1;j <= n;j ++)
73             e.push_back((node){a[i] + a[j], i, j});
74     sort(e.begin(), e.end());
75     for(int i = 1;i <= m;i ++)
76         for(int j = i + 1;j <= m;j ++) {
77             ll k = b[i] + b[j];
78             node tt = find1(qaq + k * 2);
79             if(tt.y != -1 && abs(qaq + k * 2 - tt.x * 2) < abs(tmp)) tmp = qaq + k * 2 - tt.x * 2, a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
80             tt = find2(qaq + k * 2);
81             if(tt.y != -1 && abs(qaq + k *  2 - tt.x * 2) < abs(tmp)) tmp = qaq + k * 2 - tt.x * 2, a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
82         }
83     if(abs(qaq) <= abs(dis) && abs(qaq) <= abs(tmp)) printf("%lld\n0\n", abs(qaq));
84     else if(abs(dis) <= abs(tmp)) printf("%lld\n1\n%d %d", abs(dis), t1, t2);
85     else printf("%lld\n2\n%d %d\n%d %d", abs(tmp), a1, a2, a3, a4);
86     return 0;
87  }
View Code

補題的時候,就把比賽代碼三個地方的 int 改成了long long就過了

?

C.別人補題寫的DP一眼看不懂...反正數據范圍也不大,我們來xjb亂搞吧

數據范圍20,時間5s,沒有直接爆搜的思路

答案在0-1之間,滿足單調性...試試二分?那judge呢?暴力dfs枚舉?

效率玄學?并沒有關系!...就當試試咯...過了...比DP還快...

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 const double eps = 1e-14;
 8 
 9 int n, L[21], R[21];
10 
11 double a[21];
12 
13 bool dfs(int i, int x) {
14     if(i > n)
15         return 1;
16     int y = L[i] / x;
17     if(L[i] % x) y ++;
18     for(y *= x;y <= R[i];y += x)
19         if(dfs(i + 1, y))
20             return 1;
21     return 0;
22 }
23 
24 bool judge(double k) {
25     for(int i = 1;i <= n;i ++)
26         L[i] = ceil(a[i] - a[i] * k), R[i] = floor(a[i] + a[i] * k);
27     for(int i = L[1];i <= R[1];i ++)
28         if(dfs(2, i))
29             return 1;
30     return 0;
31 }
32 
33 int main() {
34     scanf("%d", &n);
35     for(int i = 1;i <= n;i ++)
36         scanf("%lf", &a[i]);
37     double l = 0, r = 1.0, mid, ans;
38     for(int t = 66;t --;) {
39         mid = (l + r) / 2;
40         if(judge(mid)) ans = mid, r = mid - eps;
41         else l = mid + eps;
42     }
43     printf("%.12f", ans);
44     return 0;
45 }
View Code

看了別人DP代碼...看懂了...

f[i][j]代表把第 i 種貨幣變成 j 的最小答案

我們有一種無賴解是把所有貨幣都變0

所以對于第 i 種貨幣,從 0 枚舉到 a[i] * 2 就可以啦

復雜度O(nklogk), ?k = max(a[i]) = 20W

這么來說粗略計算我的做法復雜度就是O(nklogk * log(1/eps))...實際優太多

?

D.ans = C(n,3) - 不合法的三角形

對于非法三角形枚舉最大邊 z

再求 x + y <= z 的 (x,y) 有多少對即可

預處理,O(1)回答

 1 #include <cstdio>
 2 
 3 typedef long long ll;
 4 
 5 const int maxn = 1000010;
 6 
 7 ll a[maxn], b[maxn];
 8 
 9 ll c(ll x) {
10     return x * (x - 1) * (x - 2) / 6;
11 }
12 
13 int main() {
14     for(int i = 3;i <= 1000000;i ++) a[i] = (i + 1) / 2 - 1;
15     for(int i = 4, j = 0;i <= 1000000;i ++) {
16         if(!(i & 1)) j ++;
17         b[i] = b[i - 1] + j;
18     }    
19     for(int i = 3;i <= 1000000;i ++) a[i] += a[i - 1], b[i] += b[i - 1];
20     int n;
21     while(scanf("%d", &n), n >= 3) printf("%lld\n", c(n) - a[n] - b[n]);
22     return 0;
23 }
View Code

?

E.

轉載于:https://www.cnblogs.com/ytytzzz/p/7253450.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/455579.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/455579.shtml
英文地址,請注明出處:http://en.pswp.cn/news/455579.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python02期(北京)課程筆記索引

day01 初始python關于使用notepad運行python程序注釋和語句分類 day02 命名方式和關鍵字數據類型數據類型轉換 day03 變量與數據類型運算和運算符進制轉換 day04 循環結構 day05 函數概述 day06 nonlocal和global 關鍵字詳解 day07 python核心,內建函數高階函數字…

python常用快捷鍵、寫代碼事半功倍_Pycharm常用快捷鍵總結及配置方法

工欲善其事必先利其器&#xff0c;Python開發利器Pycharm常用快捷鍵以及配置如下&#xff0c;相信有了這些快捷鍵&#xff0c;你的開發會事半功倍 一 常用快捷鍵 編輯類&#xff1a; Ctrl D 復制選定的區域或行 Ctrl Y 刪除選定的行 Ctrl Alt L 代碼格式化 Ctrl Alt O 優…

PHP中的魔術常量

魔術常量 PHP 向它運行的任何腳本提供了大量的預定義常量。不過很多常量都是由不同的擴展庫定義的&#xff0c;只有在加載了這些擴展庫時才會出現&#xff0c;或者動態加載后&#xff0c;或者在編譯時已經包括進去了。 有八個魔術常量它們的值隨著它們在代碼中的位置改變而改…

Java中的繼承性特性

繼承性是java中的第二特性之一。而繼承性最為關鍵的地方為&#xff1a;代碼重用性的問題&#xff0c;利用繼承性可以從已有的類中繼續派生出新的子類&#xff0c;也可以利用子類擴展出更多的操作功能。 繼承性的實現代碼為&#xff1a;class 子類 extends 父類{ } 有以下3點說…

10大html5前端框架

Bootstrap 首先說 Bootstrap&#xff0c;估計你也猜到會先說或者一定會有這個( 呵呵了 )&#xff0c;這是說明它的強大之處&#xff0c;擁有框架一壁江山的勢氣。自己剛入道的時候本著代碼任何一個字母都得自己敲出來擋我者廢的決心&#xff0c;來讓自己成長。結果受到周圍各 種…

多媒體技術復習匯總 收藏

多媒體技術復習匯總 收藏 1. 什么是媒體&#xff1a;媒體是信息表示和傳輸的載體。2. 媒體分類&#xff1a;感覺媒體&#xff0c;表示媒體&#xff0c;表現媒體&#xff0c;存儲媒體&#xff0c;傳輸媒體3. 多媒體技術的定義和特點&#xff1a;多媒體技…

PHP中的語法特點小結

PHP中的語法特點小結 1.PHP的變量開頭要加上$符號,見到$就知道這個是一個變量 2.PHP中的常量才是不用加$符號的 3.PHP中$可以用來嵌套使用,從而實現動態的變量名的層級調用 4.PHP程序<?php開頭,結尾可以加上?>,也可以不加 5.PHP中的常量有著魔術常量(系統自帶的) 6.PH…

滾動行為

new router({ scrollBehavior (to, from, savaPosition) { if(savePosition) { //歷史記錄的前進后退記住的之前滾動到的位置 return savePosition } else { return {x: 0, y: 0} } //history模式下 定位到某個元素失效的解決辦法 if(to.hash) { return { selector: to.h…

使用FFMPEG SDK解碼流數據獲得YUV數據及其大小

本文以H264視頻流為例&#xff0c;講解解碼流數據的步驟。 為突出重點&#xff0c;本文只專注于討論解碼視頻流數據&#xff0c;不涉及其它&#xff08;如開發環境的配置等&#xff09;。如果您需要這方面的信息&#xff0c;請和我聯系。 準備變量 定義AVCodecContext。如果您…

關于Python3.7和Python3.6中元組類型數據內存存儲問題

關于Python3.7和Python3.6中元組類型數據內存存儲問題 小編最近發現了一個瑕疵 當定義一個元組類型的變量后,若在程序后面再定義一個元組變量,這兩個元組的內容相同,那么在不同的版本中會出現不同的結果 在Python3.6版本中,解釋器將在內存中開辟兩個內存空間分別存儲兩個元組的…

shell 刪除了hdfs 文件_從零開始學大數據(三) Shell操作HDFS文件系統-中

1、格式化[rootmaster sbin]# hdfs namenode -format2、命令hdfs dfsadmin查看(hdfs dfsadmin -report)[rootmaster ~]# hdfs dfsadmin -report安全模式#獲取安全模式狀態[rootmaster ~]# hdfs dfsadmin -safemode get#進入安全狀態[rootmaster ~]# hdfs dfsadmin -safemode en…

計算機硬件

計算機硬件 一、為什么要學習計算機基礎 程序員編程的本質就是讓計算機去工作&#xff0c;而編程語言就是程序員與計算機溝通的介質。程序員要想讓計算機工作&#xff0c;就要知道計算機能干什么、是怎么樣的一個完成過程&#xff0c;這也是我們必須學習計算機基礎的原因。 …

當編程作為一種愛好

一、當編程作為一種愛好&#xff0c;時刻關心一段代碼如何實現。 二、當把工具操作得足夠熟悉&#xff0c;閉眼即能達到代碼述寫的規范。 三、程序呀&#xff0c;如果愛上你是我的錯&#xff0c;我打算一錯到底。轉載于:https://www.cnblogs.com/spiriter88/p/6913539.html

Python中的函數概述

1.python中函數概述 概念 模塊化編程的思想 有組織,可共享(重復使用,實現特定的功能的代碼塊) 提高程序的可維護性,提高開發效率,提高代碼的重用性定義一個函數 1.語法:def 函數名稱(形參列表):函數體/代碼塊return 返回值 2.定義參數介紹 def :關鍵字 用于函數的定義,函數的…

鐵路售票系統_鐵路資訊:復興號動車、智能京張高鐵…中國最高端鐵路裝備看這里...

今天上午&#xff0c;兩年一度的中國國際現代化鐵路技術裝備展在京開展&#xff0c;會期3天&#xff0c;將集中展示路網建設、客貨運輸、經營管理、工程建造、技術裝備、旅客服務等鐵路行業各領域的先進產品及技術。展會現場智能京張&#xff1a;將首次實現時速350公里自動駕駛…

H.264的NALU,RTP封包說明(轉自牛人)

H.264 RTP payload 格式 H.264 視頻 RTP 負載格式 1. 網絡抽象層單元類型 (NALU) NALU 頭由一個字節組成, 它的語法如下: --------------- |0|1|2|3|4|5|6|7| -------- |F|NRI| Type | --------------- F: 1 個比特.forbidden_zero_bit. 在 H.264 規…

CentOS下安裝MySQL報安裝文件conflicts錯誤:

2019獨角獸企業重金招聘Python工程師標準>>> 第一&#xff1a;報這個錯誤&#xff0c;說明已經安裝或相關文件已經存在&#xff0c;把已經存在的文件卸載了就可以了&#xff1a; rpm -e --nodeps mysql-libs-5.1.* 轉載于:https://my.oschina.net/u/3197158/blog/1…

inc指令是什么意思_西門子PLC一些指令

指令(英文全稱意思)∶指令含義1、LD ( Load裝載):動合觸點2、LDN (Load Not不裝載):動斷觸點3、A(And與動合):用于動合觸點串聯4、AN (And Not與動斷):用于動斷觸點串聯5、o(Or 或動合):用于動合觸點并聯6、ON(Or Not 或動斷):用于動斷觸點并聯7、(Out輸出):用于線圈輸出8、OLD…

python核心,內建函數,高階函數

晨測 global和nonlocal區別 寫一個遞歸的階乘回顧 1.global和nonlocal 關鍵字 2.函數的遞歸 1.查找規律 2.設置退出條件 3.性能 3.閉包 外函數中定義一個內函數 外函數的返回值是內函數的引用 內函數引用外函數的變量,未來外函數執行完畢,不會釋放被內函數引用變量 4.總結 1.…

對h.264壓縮視頻碼流中i幀的提取(firstime)

這個問題要說清楚還是有點復雜&#xff1a;首先判斷 NALU 類型是否是 5&#xff0c;如果是&#xff0c;那么以后連續出現的 NALU 類型為 5 的 NALU 就屬于 IDR 幀&#xff08;一種特殊的 I 幀&#xff09;&#xff1b;如果 NALU 不是 5&#xff0c;則要進一步判斷 slice_type 是…