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<iostream>
using namespace std;
#define N 100

long long ipt,f[N],pos=0,ct;

int main(){
f[1]=1;f[2]=1;
for(int i=3;i<N;i++){
f[i]=f[i-1]+f[i-2];
}

cin>>ipt;
for(int i=0;i<N;i++){
if(ipt==f[i]){
pos=i;
break;
}
}

if(pos==0){
cout<<ipt;
return 0;
}

long long ans=pos;
while(cin>>ipt){
pos++;
if(ipt!=f[pos]){
cout<<ipt;
return 0;
}
}
cout<<ans;
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
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

string str;

int main(){
int sum=0,tmp=0;
cin>>str;
for(int i=0;i<str.size()/3*3;i+=3){
tmp=0;
if(i&1){
for(int j=2;j>=0;j--){
tmp=int(str[i+j]-'0')+10*tmp;
}
}else{
for(int j=0;j<3;j++){
tmp=int(str[i+j]-'0')+10*tmp;
}
}
sum=(sum+tmp)%997;
}
tmp=0;
if((str.size()/3*3)&1){
for(int i=str.size()-1;i>=str.size()/3*3;i--){
tmp=int(str[i]-'0')+10*tmp;
}
}else{
for(int i=str.size()/3*3;i<str.size();i++){
tmp=int(str[i]-'0')+10*tmp;
}
}
sum=(sum+tmp)%997;
cout<<sum;
return 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;
}
閱讀全文 »