

A Square Inequality
直接按照題目意思模擬即可。
B Intersection
枚舉 ,因?yàn)榉秶挥?
,可以暴力。
#include<bits/stdc++.h>
using namespace std;
#define maxn 105
typedef long long ll;
ll n,a[maxn],b[maxn];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++) cin>>b[i];
ll counter=0;
for(int i=1;i<=1000;i++){
bool flag=false;
for(int j=1;j<=n;j++)
if(! (a[j]<=i && i<=b[j]))
flag=true;
if(!flag) counter++;
}
cout<<counter<<endl;
return 0;
}
C IPFL
比以往難很多,考場(chǎng)上卡了好久最后才 A。
我們知道,變換兩次之后字符串其實(shí)是不變的,也就是說,我們可以嘗試先不考慮變換。
比如,我們先把一個(gè)長度為 的字符串的第
個(gè)和第
個(gè)字符交換;然后變換一次。
此時(shí),第 個(gè)字符去了第
處,第
個(gè)字符同理!
也就是說,在這一次操作之前翻轉(zhuǎn)了奇數(shù)次,這一次操作的 要進(jìn)行上述變換(注意,余數(shù)可能為
,特判一下讓它變成
)。
最后,如果一共反轉(zhuǎn)了奇數(shù)次,還是需要變換一次。
這個(gè)難度和以往的 D 差不了多少。
考試的時(shí)候最開始想成了交換對(duì)后面的變換的影響導(dǎo)致錯(cuò)誤,以后要細(xì)心一點(diǎn)(差點(diǎn)就沒看出來,導(dǎo)致時(shí)間較晚)??磥憩F(xiàn)在的 C 不可小瞧。但還是要自信!
代碼:
#include<bits/stdc++.h>
using namespace std;
string str;
int n,q,t,a,b;
int main(){
cin>>n>>str>>q;
bool sum=false;
while(q--){
cin>>t>>a>>b;
if(t==1){
if(sum){
a+=n-1; a%=2*n; a++;
b+=n-1; b%=2*n; b++;
}
char temp=str[a-1];
str[a-1]=str[b-1];
str[b-1]=temp;
}else{
sum=!sum;
}
}
if(sum) cout<<str.substr(n,n)+str.substr(0,n);
else cout<<str;
return 0;
}
D RGB Coloring 2
ABC198 的 D 也是搜索考場(chǎng)沒做出來……因?yàn)橄霃?fù)雜了
有了上次的經(jīng)驗(yàn),我決定自信地試一下暴力 dfs :
定義函數(shù) 表示把
這些點(diǎn)染色的方案數(shù);
如果可以染成紅色,那么標(biāo)記,往 爆搜;其他兩種顏色同理。
然后你發(fā)現(xiàn) TLE 了??紤]優(yōu)化:
如果 點(diǎn)沒有連邊,直接返回
,因?yàn)檫@個(gè)點(diǎn)不受控。就過了。
代碼:
#include<bits/stdc++.h>
using namespace std;
#define maxn 2010
#define int long long
struct Edge{
int to,nxt;
}edge[maxn];
int head[maxn],tot;
void add(int u,int v){
tot++;
edge[tot].to=v;
edge[tot].nxt=head[u];
head[u]=tot;
}
int n,m,u,v;
int col[maxn],counter;
bool isok(int x,int color){
for(int i=head[x];i;i=edge[i].nxt){
int tmp=edge[i].to;
if(col[tmp]==color) return false;
}
return true;
}
int dfs(int x){
if(x==n+1) return 1;
int ord=0;
for(int i=head[x];i;i=edge[i].nxt){
ord++;
}
if(ord==0) return 3*dfs(x+1);
int cnt=0;
for(int i=1;i<=3;i++){
if(isok(x,i)){
col[x]=i;
cnt+=dfs(x+1);
col[x]=0;
}
}
return cnt;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u>>v;
add(u,v); add(v,u);
}
cout<<dfs(1);
return 0;
}
E Permutation
賽場(chǎng)上沒時(shí)間了沒做出來,賽后做也沒做對(duì)。反正是狀壓,有空補(bǔ)。