0%

基本上就是照著題目敘述稀里糊塗寫在 if 裡面就可以了,我還用了前綴和,這樣比較方便算區間和。

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
#include<iostream>
using namespace std;

int main(){
int k[20]={0},p[20]={0};
for(int i=1;i<=15;i++){
cin>>k[i];
p[i]=p[i-1]+k[i];
}
for(int i=11;i<=15;i++){
cout<<k[i];
if((p[i]-p[i-10]) > (p[i-1]-p[i-1-10]) &&
(p[i]-p[i-5]) > (p[i-1]-p[i-1-5]) &&
(k[i]*5> (p[i]-p[i-5]) && (p[i]-p[i-5])*2 > (p[i]-p[i-10])) &&
(5*k[i]*10 < 6*(p[i]-p[i-10])*10/10) ){
cout<<"B";
}else if(5*k[i]*10 > 6*(p[i]-p[i-10])*10/10 ||
(k[i]*10< (p[i]-p[i-5])*10/5 && (p[i]-p[i-5])*2 < (p[i]-p[i-10])) ||
(p[i]-p[i-10]) < (p[i-1]-p[i-1-10]) ){
cout<<"S";
}else{
cout<<"N";
}
cout<<" ";
}
return 0;
}
閱讀全文 »

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
#include<bits/stdc++.h>
using namespace std;

int main(){
int k,m,n;
cin>>k>>m>>n;
vector<int> a[55],ps[55];

for(int i=0;i<n;i++) ps[0].push_back(0);
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
int tmp;cin>>tmp;
a[i].push_back(tmp);
ps[i].push_back(tmp);
}
}
for(int i=1;i<=m;i++){
for(int j=0;j<n;j++){
ps[i][j]+=ps[i-1][j];
}
}

int best=0;
for(int i=0;i<m;i++){
for(int j=i+1;j<=m;j++){
set<int> s({0});
int psum=0;
for(int o=0;o<n;o++){
psum+=(ps[j][o]-ps[i][o]);
auto it=s.lower_bound(psum-k);
if(it!=s.end()){
best=max(best,psum-(*it));
}
s.insert(psum);
}
}
}
cout<<best<<"\n";
return 0;
}

用heap(priority_queue)實作

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
#include<bits/stdc++.h>
using namespace std;
#define INF 100000000
#define P pair<int,int>
#define F first
#define S second
#define PB push_back

P sl[1000000];

int main(){
ios::sync_with_stdio(0),cin.tie(0);

int t;
cin>>t;
for(int iptNum=0;iptNum<t;iptNum++){
int n,k,r=INF;

priority_queue<int> pq;

cin>>n>>k;
for(int i=0;i<n;i++){
cin>>sl[i].F>>sl[i].S;
}

sort(sl,sl+n);

for(int i=0;i<n;i++){
pq.push(sl[i].S);
if(pq.size()==k){
r=min(r,sl[i].F+pq.top());
pq.pop();
}
}
cout<<r<<"\n";
}
return 0;
}
閱讀全文 »

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;

set<int> g[100010];
int ind[100010];
int n,m;

int main(){
ios::sync_with_stdio(0);cin.tie(0);

int t;
cin>>t;
while(t--){
ans="";ct=0;
for(int i=0;i<100010;i++){
g[i].clear();
ind[i]=0;
v[i]=0;
}

cin>>n>>m;
for(int i=0;i<m;i++){
int to,from;
cin>>from>>to;
g[from].insert(to);
}

for(int i=0;i<n;i++)
for(auto a:g[i])
ind[a]++;
vector<int> ans;
priority_queue<int,vector<int>,greater<int>> topo;
for(int i=0;i<n;i++){
if(ind[i]==0){
topo.push(i);
}
}

while(!topo.empty()){
int now=topo.top();
topo.pop();
ans.push_back(now);
for(auto next:g[now]){
if(--ind[next] ==0){
topo.push(next);
}
}
}

if(ans.size()==n){
cout<<ans[0];
for(int i=1;i<ans.size();i++) cout<<" "<<ans[i];
cout<<"\n";
}else{
cout<<"QAQ\n";
}
}
return 0;
}

依題目用模擬即可,以下為程式碼

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;

bitset<2500> tree;

int main(){
ios::sync_with_stdio(false);cin.tie(0);

int iptNum;
cin>>iptNum;
while(iptNum--){
int d,n;
tree.set();
cin>>d>>n;

int pos;
for(int i=0;i<n;i++){
pos=1;
for(int j=1;j<d;j++){
if(tree[pos]){
tree[pos]=false;
pos*=2;
}else{
tree[pos]=true;
pos=pos*2+1;
}
}
}
cout<<pos<<"\n";
}
return 0;
}
閱讀全文 »

基本的因數性質

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
#include<bits/stdc++.h>
using namespace std;

int main(){
ios::sync_with_stdio(false);

int dt[1000];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>dt[i];

}
stable_sort(dt,dt+n,[](int a,int b){
int ctA=0,ctB=0;

while(a>=1){
if(a%2==1){
ctA++;
}
a/=2;
}
while(b>=1){
if(b%2==1){
ctB++;
}
b/=2;
}
return ctA<ctB;
});

for(int i=0;i<n;i++){
cout<<dt[i]<<" ";
}
return 0;
}
閱讀全文 »

用 priority_queue 與 unordered_map 。

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
#include<bits/stdc++.h>
using namespace std;

int main(){
ios::sync_with_stdio(false);cin.tie(0);

priority_queue<int> mx;
priority_queue<int,vector<int>,greater<int>> mn;
unordered_map<int,int> s;

int t;
while(cin>>t){
if(t==0) break;

if(t==-1){
while(!mn.empty() && !s[mn.top()]){
mn.pop();
}
if(!mn.empty()){
cout<<mn.top()<<' ';
s[mn.top()]--;
mn.pop();
}
}else if(t==-2){
while(!mx.empty() && !s[mx.top()]){
mx.pop();
}
if(!mx.empty()){
cout<<mx.top()<<' ';
s[mx.top()]--;
mx.pop();
}
}else{
s[t]++;
mx.push(t);
mn.push(t);
}
}
return 0;
}
閱讀全文 »