課堂重點

用STL的sort要用 .begin().end() ,可以用 .begin()+d 的方式排序一個區間
cerr可以用來debug,且會被judge忽略,不會造成忘記註解的錯誤

一秒的時間c/c++大約可以執行 10^8 個指令,在計算時間複雜度的時候要用這個估算

題解

APCS 2017-0304-4基地台

題目連結:https://zerojudge.tw/ShowProblem?problemid=c575

AC code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k;
vector<int> s;
bool isCover(int d){
int tempk=k,pos=0;
for(int i=0;i<n;++i){
if(!tempk)
return pos>s[n-1];
if(pos>s[i])
continue;
else
pos=s[i]+d,tempk--;
}
return (pos+d*tempk)>s[n-1];
}

signed main(){
cin>>n>>k;
s.resize(n);
for(int &i: s)
cin>>i;
sort(s.begin(),s.end());
int l=0,r=1000000002,m;
while(l<r){
m=(l+r)/2;
if(isCover(m))
r=m;
else
l=m+1;
}
if(isCover(r-2))
cout<<r-3<<"\n";
else if(isCover(r-1))
cout<<r-2<<"\n";
else if(isCover(r))
cout<<r-1<<"\n";
else
cout<<r<<endl;
return 0;
}

二分搜尋法

題目連結:https://zerojudge.tw/ShowProblem?problemid=d732

AC code(一般寫法):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int a[N],q[N];
int main(){
int n,k;
cin>>n>>k;
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<n;++i)
cin>>q[i];
for(int i=0;i<n;++i){
int l=0,r=n,m;
bool flag=0;
while(l<r){
m=(l+r)/2;
if(a[m]==q[i]){
flag=1;
break;
}
else if(a[m]>q[i])
r=m;
else
l=m+1;
}
if(flag)
cout<<m+1<<"\n";
else
cout<<0<<"\n";
}
return 0;
}

AC code(lower_bound 陣列寫法):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int a[N],q[N];
int main(){
int n,k;
cin>>n>>k;
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<n;++i)
cin>>q[i];
for(int i=0;i<n;++i){
auto it=lower_bound(a,a+n,q[i]);
if(*it==q[i])
cout<<it-a+1<<"\n";
else
cout<<0<<"\n";
}
return 0;
}

AC code(lower_bound vector寫法):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
vector<int> a,q;
int main(){
int n,k;
cin>>n>>k;
a.resize(n),q.resize(k);
for(int &i: a)
cin>>i;
for(int &i: q)
cin>>i;
for(int i:q){
auto it=lower_bound(a.begin(),a.end(),i);
if(*it==i)
cout<<it-a.begin()+1<<"\n";
else
cout<<0<<"\n";
}
return 0;
}

切割費用

題目連結:https://zerojudge.tw/ShowProblem?problemid=f607

AC code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,L,order[200000],i,o,p;//o means order,p means pos
long long int sum=0;
cin>>n>>L;
set<int> cut{0,L};
for(i=0;i<n;++i)
cin>>p>>o,order[o-1]=p;
for(i=0;i<n;++i){
auto lb=cut.lower_bound(order[i]);
sum+=*lb-*prev(lb);
cut.insert(order[i]);
}
cout<<sum<<endl;
}

新手訓練系列 ~ 圖論

題目連結:https://zerojudge.tw/ShowProblem?problemid=a290

AC code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
vector<vector<int>> g;
int vis[1000]={0};
void r(int u){
if(vis[u]) return ;
vis[u]=1;
for(int i:g[u]){
if(!vis[i])
r(i);
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int u,v;
while(cin>>n>>m){
g.clear();
g.resize(n+5);
memset(vis,0,sizeof(vis));
for(int i=0;i<m;++i){
cin>>u>>v;
g[u].push_back(v);
}
cin>>u>>v;
r(u);
if(vis[v])
cout<<"Yes!!!\n";
else
cout<<"No!!!\n";
}
return 0;
}

迷宮問題#1

題目連結:https://zerojudge.tw/ShowProblem?problemid=a982

AC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> v;
using namespace std;
int main() {
int n,i,dx[]={0,0,-1,1},dy[]={-1,1,0,0},row,col;//上下左右
pair<int,int> cur;
while(cin>>n){
vector<string> m(n);//m: map
vector<v> r(n, v(n,-1));//r: record
for(i=0;i< n;++i)
cin>>m[i];
deque<pair<int,int>> bfs{make_pair(1, 1)};
r[1][1]=1;
while(bfs.size()){
cur=bfs.front(),bfs.pop_front();
for(i=0;i<4;++i){
row=cur.first+dx[i];
col=cur.second+dy[i];
if(m[row][col]=='.'&&r[row][col]==-1){
m[row][col]='#';
bfs.push_back(make_pair(row,col));
r[row][col]=r[cur.first][cur.second]+1;
}
}
}
if(r[n-2][n-2]==-1)
cout<<"No solution!\n";
else
cout<<r[n-2][n-2]<<"\n";
}
}

小畫家真好用

題目連結:https://zerojudge.tw/ShowProblem?problemid=d626

AC code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
int n,i,j,dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};
vector<string> p;
void DFS(int i,int j)
{
p[i][j]='+';
for(int k=0;k<4;++k)
{
if(i+dy[k]>=0&&i+dy[k]<n&&j+dx[k]>=0&&j+dx[k]<n&&p[i+dy[k]][j+dx[k]]=='-')
DFS(i+dy[k],j+dx[k]);
}
}
int main() {
cin>>n;
p.resize(n);
for(string &s:p)
cin>>s;
cin>>i>>j;
DFS(i,j);
for(auto s:p)
cout<<s<<"\n";
}

無營利 僅供分享

如果有不會的 午休時間我在資訊教室!