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

今天(準確說是昨天,一下子就過12點了)下午剛參加了CSP認證考試,大概是考了220(前兩題AC,第三題太折磨了懶得看了,后面兩題各混了10分),唯一有點參與感的就是B題了,于是這里分析下我的B題思路。

題意:對于n?nn*nn?n的矩陣,求存在多少個這樣的點(以該點為中心半徑為 rrr 正方形塊中的總值的平均數不大于閾值 ttt
在這里插入圖片描述
思路:如上圖所示,靠邊緣的點所形成的方塊是不完整的,當時第一遍我是打算用dp計算中央部分完整的塊,而對于邊緣不完整的塊則采用暴力,實現起來比較簡單,沒想到的是有30%(n<=600,r<=100n<=600,r<=100n<=600,r<=100)TLE了,碼代碼前只是粗略的計算了一下時間復雜度,感覺能過。于是又想到了下面的方法,消除了暴力的部分。
在這里插入圖片描述
人為地將矩陣向外擴充 rrr 個單位,這樣就避免了靠近邊緣的點形成不完整的方塊,并且對于擴充的格子均填上閾值 ttt ,這樣在計算平均值時又保持了原方塊的平均值與閾值的大小關系

summ≤t→sum≤m?tsum+(n?t)≤m?t+(n?t)sum+(n?t)≤(m+n)?tsum+n?tm+n≤t\frac{sum}{m} \le t \to sum \le m*t \\ sum+(n*t) \le m*t+(n*t) \\ sum+(n*t) \le (m+n)*t \\ \frac{sum+n*t}{m+n} \le tmsum?tsumm?tsum+(n?t)m?t+(n?t)sum+(n?t)(m+n)?tm+nsum+n?t?t

dp的部分有空再分析分析,現在得睡覺了。。

代碼:(賽后重寫了一遍,沒交過,不保證AC)

#include<bits/stdc++.h>
#define debug1(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
const int N = 1000;
using namespace std;
int a[N][N], dp[N][N],row[N],col[N][N];
int n,L,r,t,R;
void init()
{for(int i = 0; i < N; i++){row[i] = -1;for(int j = 0; j < N; j++){a[i][j] = t;col[i][j] = -1;}}
}
int calc_row(int i)
{if(row[i] == -1){int sum = 0;for(int j = 0 - r; j <= 0 + r; j++) sum += a[i][R + j];row[i] = sum;}return row[i];
}
int calc_col(int i, int j)
{if(col[i - 1][j] == -1){int sum = 0;for(int k = i - r; k <= i + r; k++) sum += a[k][j];col[i][j] = sum;}else{col[i][j] = col[i - 1][j] + a[i + r][j] - a[i - 1 - r][j];}return col[i][j];
}
int main()
{cin>>n>>L>>r>>t;R = r + 5;init();for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){scanf("%d",&a[R + i][R + j]);}}// calculate dp[0][0]for(int i = 0 - r; i <= 0 + r; i++){for(int j = 0 - r; j <= 0 + r; j++){dp[0][0] += a[R + i][R + j];}}for(int i = 0; i < n; i++){if(i){int sub_row = calc_row(R + i - 1 - r);int add_row = calc_row(R + i + r);dp[i][0] = dp[i - 1][0] + add_row - sub_row;}for(int j = 1; j < n; j++){int sub_col = calc_col(R + i, R + j - 1 - r);int add_col = calc_col(R + i, R + j + r);dp[i][j] = dp[i][j - 1] + add_col - sub_col;}}int ans = 0, block = (2 * r + 1)*(2 * r + 1);for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){if(dp[i][j] <= t * block) ans++;}}cout<<ans<<"\n";return 0;
}

剛去學習了一下二維前綴和的知識,果然比自己寫的dp簡約多了!

代碼:采用二維前綴和+人為擴矩(依舊不保證正確,目前還沒機會交)

#include<bits/stdc++.h>
#define debug1(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
const int N = 1000;
using namespace std;
int a[N][N], dp[N][N], s[N][N];
int n,L,r,t,R;
void init()
{for(int i = 0; i < N; i++){for(int j = 0; j < N; j++){a[i][j] = t;}}
}int main()
{cin>>n>>L>>r>>t;R = r + 5;init();for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){scanf("%d",&a[R + i][R + j]);}}for(int i = 0 - r; i < n + r; i++){for(int j = 0 - r; j < n + r; j++){s[R + i][R + j] = s[R + i - 1][R + j]+ s[R + i][R + j - 1]- s[R + i - 1][R + j - 1]+ a[R + i][R + j];}}int i1,j1,i2,j2,sum;int ans = 0, block = (2 * r + 1)*(2 * r + 1);for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){i1 = R + i - r; i2 = R + i + r;j1 = R + j - r; j2 = R + j + r;sum = s[i2][j2]- s[i2][j1 - 1]- s[i1 - 1][j2]+ s[i1 - 1][j1 - 1];if(sum <= t * block) ans++;}}cout<<ans<<"\n";return 0;
}

A題:比較基礎的統計題,按題目要求統計每個數出現的次數即可

#include<bits/stdc++.h>
#define debug1(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int main()
{int n, m, L;cin>>n>>m>>L;vector<int> ans(L);int tmp;for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){scanf("%d",&tmp);ans[tmp]++;}}for(int i = 0; i < L; i++) cout<<ans[i]<<" ";return 0;
}

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

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

相關文章

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以后發現有人已經…

疊筐

Problem Description 需要的時候&#xff0c;就把一個個大小差一圈的筐疊上去&#xff0c;使得從上往下看時&#xff0c;邊筐花色交錯。這個工作現在要讓計算機來完成&#xff0c;得看你的了。 Input 輸入是一個個的三元組&#xff0c;分別是&#xff0c;外筐尺寸n&#xff…

“Visual Studio.net已檢測到指定的Web服務器運行的不是Asp.net1.1版。您將無法運行Asp.net Web應用程序或服務”問題的解決方案...

解決方案一&#xff1a; 1.確定有安裝.net framework 1.1&#xff0c;可以查看目錄&#xff0c;c:\winnt\microsoft.net\framework重啟IIS&#xff0c;重啟計算機&#xff08;常規糾錯方法&#xff09; 2.如果你的Web服務器使用了固定IP&#xff1a;確定你的“Internet信息服務…

【桶】220.存在重復元素 III 【LeetCode】

220.存在重復元素 III 【LeetCode】 給你一個整數數組 nums 和兩個整數 k 和 t。請你判斷是否存在 兩個不同下標i和j&#xff0c;使得 abs(nums[i] - nums[j]) < t&#xff0c;同時又滿足 abs(i - j) < k。 如果存在則返回 true&#xff0c;不存在返回 false。 示例 1…

遠控免殺專題12--Green-Hat-Suite免殺

0x01 免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/meterperter/reverse_tcp模塊生成。 3、由于本機測試時只是安裝了360全…

英語基礎語法(八)-時態

英語中&#xff0c;動詞時態的用法是尤其復雜和富于變化的。經常通過動詞詞尾、組動詞等的變化表明動作發生時間的先后順序&#xff0c;即時態。總的來說&#xff0c;英語中的動詞時態分為 三個基本類型&#xff1a; 現在、過去和將來。動詞時態的變化常常伴隨著相應的表示時間…

Java PushbackInputStream markSupported()方法與示例

PushbackInputStream類markSupported()方法 (PushbackInputStream Class markSupported() method) markSupported() method is available in java.io package. markSupported()方法在java.io包中可用。 markSupported() method is used to check whether this stream supports …

面型對象 (接口與類的區別)

public class Demo4_Interface {public static void main(String[] args) {某女星 clown new 某女星();clown.潛規則();clown.關系();} }/*親爹只有一個&#xff0c;是單繼承;干爹可以有很多個&#xff0c;是多實現;*/ interface 某干爹{public void 關系();public void 潛規…

遠控免殺專題 13----zirikatu免殺

0x01 免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/meterperter/reverse_tcp模塊生成。 3、由于本機測試時只是安裝了360全…

UML 的九種模型圖

1. UML的模型圖 UML 的模型圖能夠將被建模的系統的某一個方面的某一部分以圖形的方式表示出來&#xff0c;不同的視圖通過將多個不同的模型圖有機組合在一起就能夠描述系統模型的某方面的特征。UML的模型圖是有模型元素構成的&#xff0c;模型元素以圖標的形式直觀形象的表達…