【競賽題解】2021年廣東工業大學第十五屆文遠知行杯程序設計競賽(同步賽)

B 找山坡

題意:在數組中找到兩相等元素相距最大的距離且這兩元素間的元素都不小于該兩端值

思路:采用單調棧

在這里插入圖片描述

例如:a[] = { 2 3 5 4 6 3 },棧內存儲元素的坐標(從1開始),便于計算距離

  1. 首先將a[1]a[2]a[3]依次入棧
  2. 當到a[4]時,發現其小于棧頂元素(a[4] < a[stack.top()]),由于a[3]大于a[4],這樣一來a[3]無論如何都不能與之后的元素相匹配,因為若是a[3]a[i] (=5)匹配,但兩者之間一定會夾著小于它們的元素a[4],因此不滿足條件將a[3]出棧,接著繼續檢查a[4]與棧頂元素的大小關系,直至a[4] >= stack.top(),否則便將棧頂出棧
  3. 接著a[5]入棧,a[6]小于棧頂于是執行步驟2操作,最后到a[6] = a[stack.top()],此時說明可以匹配兩者,記錄a[6]a[2]的坐標差,保留a[2]在棧中而a[6]不入棧,因為目前a[2]右側的元素均不小于它,即a[2]還有機會被匹配,且a[2]更靠左這樣計算出的結果也會最大化
#include<bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0)
const int N = (int)1e6+10;
using namespace std;
stack<int> st;
int a[N];
int main()
{fastio();int n;cin >> n;int ans = 0;for (int i = 1; i <= n; i++){cin >> a[i];if (st.empty() || a[i] > a[st.top()]){st.push(i);}else{while (!st.empty() && a[i] < a[st.top()]) st.pop();if (st.empty()) st.push(i);else if (a[i] == a[st.top()]){ans = max(ans, i - st.top());}else if (a[i] > a[st.top()]){st.push(i);}}}cout << ans << "\n";return 0;
}

E 撿貝殼

題意:對于一個整型數組a,每次詢問[L,R][L,R][L,R]之間為x的倍數的元素的個數

數據范圍:數組長度 n:[0,105]n : [0,10^{5}]n:[0,105] ,詢問次數 q:[0,5?104]q : [0,5*10^{4}]q:[0,5?104],因數x:[1,105]x :[1,10^5]x:[1,105]

思路:思考若采用暴力,時間復雜度為O(n?q)O(n * q)O(n?q)10910^9109的數量級顯然會超時,因此對這種多次詢問的問題不妨采用分塊的思想,將每n\sqrt{n}n?個元素作為一塊,因此數組大致可分為n\sqrt{n}n?塊(數組末尾有時不能組成完整的一塊),分塊后首先進行預處理,記錄每塊內對于因數x:[1,105]x:[1,10^5]x:[1,105]詢問對應該塊內元素的結果個數(枚舉塊內元素a[i],找出a[i]所有的因數,時間復雜度為O(ai)O(\sqrt{a_i})O(ai??)),因此預處理的總時間復雜度為O(n?ai)O(n*\sqrt{a_i})O(n?ai??)。此后對于一次詢問,較一般的情況是[L,H)+[H,T)+[T,R][L,H) +[H,T)+[T,R][L,H)+[H,T)+[T,R],其中[L,H)[L,H)[L,H)[T,R][T,R][T,R]屬于某一塊一部分,因此對于這兩部分采取暴力求解,而[H,T)[H,T)[H,T)則屬于多個連續的完整的塊,可以直接利用預處理的結果,其他的詢問情況討論分析即可,全部詢問的時間復雜度為O(q?(n+2n))O(q*(\sqrt{n} + 2\sqrt{n}))O(q?(n?+2n?))也即O(q?n)O(q*\sqrt{n} )O(q?n?),其中的2n2\sqrt{n}2n?理解為暴力求解的部分

在這里插入圖片描述

下面貼的是標程,自己第一次寫的分塊有點不堪入目

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int sum[320][maxn];
int arr[maxn];
int n, q;
void blocked() {int sz = sqrt(n);for (int i = 0; i < n; ++i) {for (int j = 1; j * j <= arr[i]; ++j) {if (arr[i] % j == 0) {sum[i / sz][j]++;if (arr[i] / j != j) sum[i / sz][arr[i] / j]++;}}}
}
int qurey(int l, int r, int x) {int sz = sqrt(n);int ans = 0;for (int i = l; i <= r && i / sz == l / sz; ++i) {if (arr[i] % x == 0) ans++;}if (l / sz == r / sz) return ans;for (int i = r / sz * sz; i <= r; ++i) {if (arr[i] % x == 0) ans++;}for (int j = l / sz + 1; j <= r / sz - 1; ++j) {ans += sum[j][x];}return ans;
}
int main() {while (~scanf("%d%d", &n, &q)) {memset(sum, 0, sizeof(sum));for (int i = 0; i < n; ++i) {scanf("%d", arr + i);}blocked();while (q--) {int l, r, x;scanf("%d%d%d", &l, &r, &x);l--, r--;printf("%d\n", qurey(l, r, x));}}return 0;
}

自己的代碼:

#include<bits/stdc++.h>
#define debug1(x) cout<<#x<<":"<<x<<endl
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
typedef unsigned long long ull;
const int N = 100010;
using namespace std;
int a[N];
int mp[320][N];
void Factor(int i,int block_no)
{int j = 1;for(; j * j < a[i]; j++){if(a[i] % j == 0){mp[block_no][j]++;mp[block_no][a[i] / j]++;}}if(j * j == a[i]) mp[block_no][j]++;
}
int main()
{int n,q;cin>>n>>q;for(int i = 0; i < n; i++) scanf("%d",&a[i]);int block_size = sqrt(n);int block_no_max;for(int i = 0; (i + 1) * block_size - 1 < n; i++){for(int j = i * block_size; j < (i + 1) * block_size; j++){Factor(j,i);}block_no_max = i;}while(q--){int ans = 0;int l,r,x;scanf("%d%d%d",&l,&r,&x);l--;r--;int head = l / block_size;int tail = r / block_size;if(head == tail){if(r - l + 1 == block_size) ans += mp[head][x];else{for(int i = l; i <= r; i++){if(a[i] % x == 0) ans++;}}printf("%d\n",ans);continue;}if(l % block_size){for(int i = l; i < (head + 1) * block_size; i++){if(a[i] % x == 0) ans++;}head++;}if((r + 1) % block_size){for(int i = tail * block_size; i <= r; i++){if(a[i] % x == 0) ans++;}tail--;}for(int i = head; i <= tail; i++){ans += mp[i][x];}printf("%d\n",ans);}return 0;
}

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

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

相關文章

[轉]JAVA AES 加密算法

本文轉自&#xff1a;http://blog.sina.com.cn/s/blog_7c8eb1590100svr0.html package com.siro.tools;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64E…

java中Scanner類中 next()與nextLine()的區別

問題&#xff1a;提示用戶輸入一個英文字符串或者要解密的字符串&#xff0c;然后通過掃描儀獲取用戶輸入的字符串&#xff0c;經過加密或者解密后&#xff0c;把字符串輸出。 import java.util.Scanner;public class Encryption {public static void main(String[] args) {Sca…

操作系統中的處理機調度調度_操作系統中的流程分類和調度

操作系統中的處理機調度調度處理 (Process) In the operating system, there are numerous task and application program run simultaneously. A program is stored in the hard disk or any other form of secondary storage. When the program is executed it must be loade…

NX機制及繞過策略-ret2libc

程序&#xff1a; 1.c #include <stdio.h> void exploit() {system("/bin/sh"); } void func() {char str[0x20];read(0,str,0x50); } int main() {func();return 0; }0x01 NX介紹 溢出攻擊的本質在于馮諾依曼計算機模型對數據和代碼沒有明確區分這一先天性缺…

網站SEO策略的制定

在對一個網站做SEO的時候&#xff0c;SEO技術水平類似的人&#xff0c;營銷效果可能天壤之別&#xff0c;這是因為網站SEO策略的制定的不同&#xff01;-----這個是最根本的。 SEO技術非常的簡單&#xff0c;因為SEO不是去尋找搜索引擎的漏洞&#xff0c;而是根據搜索引…

Python | 程序從列表中刪除范圍內的所有元素

Given a list and we have to remove elements in a range from the list in Python. 給定一個列表&#xff0c;我們必須從Python中的列表中刪除范圍內的元素。 刪除列表(開始索引&#xff0c;結束索引) (del list(start_index, end_index)) del() method is used to remove a…

面向對象 (接口 Interface)

1&#xff0c;面向對象(接口的概述及其特點) A:接口概述 從狹義的角度講就是指java中的interface從廣義的角度講對外提供規則的都是接口 B:接口特點 a:接口用關鍵字interface表示 interface 接口名 {}b:類實現接口用implements表示 class 類名 implements 接口名 {}c:接口…

android unbound prefix

少一個命名空間加上就行了&#xff1a;xmlns:android"http://schemas.android.com/apk/res/android" 轉載于:https://www.cnblogs.com/nizuimeiabc1/archive/2011/10/09/4254310.html

【競賽題解】第22次CCF計算機軟件能力認證 B

今天&#xff08;準確說是昨天&#xff0c;一下子就過12點了&#xff09;下午剛參加了CSP認證考試&#xff0c;大概是考了220&#xff08;前兩題AC&#xff0c;第三題太折磨了懶得看了&#xff0c;后面兩題各混了10分&#xff09;&#xff0c;唯一有點參與感的就是B題了&#x…

gbd調試64位程序關鍵

程序&#xff1a; 4.c&#xff1a; #include<stdio.h> void exploit() {system("/bin/sh"); } void main() {char buf[20];gets(buf); }編譯&#xff1a; gcc -no-pie -fno-stack-protector -m64 -o 4.exe 4.cNX保護&#xff0c;棧數據不可執行 使用命令&…

C#全局鼠標鍵盤Hook (備查)

using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace DCIEngine.FrameWork.Snap { /// <summary> /// 這個類可以讓你得到一個在…

fcfs調度算法_FCFS:先來先服務調度算法

fcfs調度算法The FCFS, which stands for First Come First Serve Scheduling Algorithm, is a non-preemptive scheduling algorithm, which means that if a process once starts executing in the processor, then it cannot be preempted in between the processing. Thus,…

親和數

Problem Description 古希臘數學家畢達哥拉斯在自然數研究中發現&#xff0c;220的所有真約數(即不是自身的約數)之和為&#xff1a; 1245101120224455110&#xff1d;284。 1* 220220&#xff1b;2* 110220&#xff1b;4* 55220&#xff1b;5* 44220&#xff1b;10*20220;…

轉:JNI jstring與c++字符串類型轉換函數

jstring與c字符串類型轉換函數 jstring str2jstring(JNIEnv* env,const char* pat) {//定義java String類 strClassjclass strClass (env)->FindClass("Ljava/lang/String;");//獲取String(byte[],String)的構造器,用于將本地byte[]數組轉換為一個新Stringjmetho…

python字符串轉浮點數_如何在Python中檢查字符串是否為數字(浮點數)?

python字符串轉浮點數Using python it is very to interconvert the datatypes of a variable. A string can be easily converted to an integer or a float. However, asserting a string to be a float is a task by itself. Python provides an option to assert if a stri…

nhibernate學習之三級聯(Ternary Associations)篇

1) 學習目標通過進一步學習Nhibernate基礎知識&#xff0c;掌握用Nhiberate實現對級聯的支持&#xff0c;通過一個簡單的用戶角色權限系統來體驗nhibernate對級聯的強大支持。2&#xff09;開發環境和必要準備 開發環境為:windows 2003,Visual studio .Net 2005,Sql server 200…

【競賽題解】Codeforces Round #715 (Div. 2) C

C. The Sports Festival 題意&#xff1a;對于給定的整型數組aaa&#xff0c;每次選擇其中一個元素aia_iai?&#xff08;不能重復選擇同一元素&#xff09;&#xff0c;每次計算已選擇的元素的極差&#xff08;最大元素減最小元素的差&#xff09;&#xff0c;輸出最后極差和…

C和匯編---sizeof運算符和strlen函數

sizeof sizeof是C語言的內置運算符&#xff0c;以字節為單位給出指定類型的大小。 程序&#xff1a; #include <stdio.h>int main(void) {int a8;int b sizeof(a);//printf("a占用字節%u\n",sizeof(a));printf("a占用字節%d\n",b);return 0; }反匯…

Java接口程序練習

題目&#xff1a; 編寫一個接口程序&#xff0c;其中定義一個計算體積的方法。然后&#xff0c;在設計應用程序實現這個接口&#xff0c;分別計算矩形柱面體積和圓形柱面體積。 代碼如下&#xff1a; import java.util.*;//導入掃描儀&#xff1b; public class clown {publi…

[原]Asp.net替換不同版本的Dll文件碰到的問題以及解決辦法.

情景還原: 今天一個朋友說網站不能上傳圖片,我檢查后發現一直卡住在上傳頁面,一直滾動,是個Fckeditor控件2.6.3的. 經過google以后得到的結論是圖片上傳成功,但是沒有返回結果,在服務器上可以看到上傳的圖片. 說明是上傳控件有問題,程序不能返回結果. 再google以后發現有人已經…