Educational Codeforces Round 1 Tutorial
題目傳送門:http://codeforces.com/contest/598
A. Tricky Sum
題意:問小於等於n的所有自然數與其係數乘積的和,其中所有2的次冪(包括0次冪)的係數為-1,其他數字係數為1。
解法:直接根據公式求出自然數前n項和,然後列舉2的冪減去兩倍即可。
#include <bits/stdc++.h>
using namespace std;
long long tt[33];
int main(){
tt[0]=1;
for(int i=1;i<=32;i++)
tt[i]=tt[i-1]*2;
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++){
long long x;
scanf("%I64d",&x);
int it=upper_bound(tt,tt+33,x)-tt;
x=x*(x+1)/2;
for(int i=0;i<it;i++)
x-=2*tt[i];
printf("%I64d\n",x);
}
return 0;
}
B. Queries on a String
題意:給你一個字串,有若干旋轉操作,(l,r,k)表示把原字串中位於[l,r]中的字元向右移動k位。問最後這個字串是什麼。
解法:根據<algorithm>標頭檔案中的rotate函式直接模擬就行,不過這個函式是向右移動,稍微變形一下即可。
#include <bits/stdc++.h>
using namespace std;
char s[10005];
int main(){
ios::sync_with_stdio(false);
cin>>s;
int m;
cin>>m;
while(m--){
int l,r,k;
cin>>l>>r>>k;
k%=(r-l+1);
rotate(s+l-1,s+r-k,s+r);
}
cout<<s<<endl;
return 0;
}
C. Nearest vectors
題意:在座標系中給你很多以原點作為端點的向量,問相互之間形成角度最小的兩個向量的編號。
解法:按照與x軸正方向形成的角度排序,比較相鄰的兩個向量更新答案。
#include <bits/stdc++.h>
using namespace std;
vector<pair<long double,int> >ang;
const double PI = acos(-1.0);
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
long double x,y;
cin>>x>>y;
ang.push_back(make_pair(atan2(y,x),i));
}
sort(ang.begin(),ang.end());
long double tg=1e9;
int ans=-1;
for(int i=1;i<ang.size();i++)
if(ang[i].first-ang[i-1].first<tg){
tg=ang[i].first-ang[i-1].first;
ans=i;
}
if(PI*2-ang[ang.size()-1].first+ang[0].first<tg)
ans=0;
printf("%d %d\n",ang[ans].second,(ans==0?ang[ang.size()-1].second:ang[ans-1].second));
return 0;
}
D. Igor In the Museum
題意:輸入一個矩陣,問從某一位置開始並可以任意移動到空位置能看到的畫有多少幅。不同位置看到的相同的畫算多個答案。
解法:dfs預處理出所有空位置的答案即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
char g[N][N];
bool vis[N][N];
int n,m,k,dx[]={1,0,-1,0},dy[]={0,1,0,-1},ans[N][N];
void dfs1(int x,int y,int& t){
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(g[xx][yy]=='*')
t++;
else if(!vis[xx][yy]&&g[xx][yy]=='.'){
vis[xx][yy]=true;
dfs1(xx,yy,t);
}
}
}
void dfs2(int x,int y,int t){
ans[x][y]=t;
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(g[xx][yy]=='.'&&ans[xx][yy]!=t)
dfs2(xx,yy,t);
}
}
int main(){
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=n;i++)
scanf(" %s",g[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(!vis[i][j]&&g[i][j]=='.'){
vis[i][j]=true;
int ans=0;
dfs1(i,j,ans);
dfs2(i,j,ans);
}
while(k--){
int x,y;
scanf("%d %d",&x,&y);
printf("%d\n",ans[x][y]);
}
return 0;
}
E. Chocolate Bar
題意:給你一塊n*m的巧克力,定義沿著某一條長度為L的線段掰開巧克力的花費是L*L,問要吃到k個單位大小的巧克力的最小花費是多少。
解法:注意到n,m,k都很小,直接記憶化搜尋即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 35;
int dp[N][N][N+20];
int dfs(int x,int y,int nd){
if(nd==0||x*y==nd)
return 0;
if(dp[x][y][nd]!=-1)
return dp[x][y][nd];
int res=0x3f3f3f3f;
for(int i=1;i<x;i++)
for(int j=0;j<=nd;j++)
if(i*y>=j&&(x-i)*y>=nd-j)
res=min(res,y*y+dfs(i,y,j)+dfs(x-i,y,nd-j));
for(int i=1;i<y;i++)
for(int j=0;j<=nd;j++)
if(x*i>=j&&x*(y-i)>=nd-j)
res=min(res,x*x+dfs(x,i,j)+dfs(x,y-i,nd-j));
return res;
}
void init(){
memset(dp,-1,sizeof dp);
for(int i=1;i<=30;i++)
for(int j=1;j<=30;j++)
for(int k=1;k<=min(i*j,50);k++)
dp[i][j][k]=dfs(i,j,k);
}
int main(){
init();
int t,n,m,k;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&n,&m,&k);
printf("%d\n",dp[n][m][k]);
}
return 0;
}
F. Cut Length
題意:輸入若干個點,問給定線段和這些點構成的多邊形相交的長度為多少。
相關文章
- Educational Codeforces Round 32
- Codeforces Educational Round#98 A
- Educational Codeforces Round 163
- 【題解】Educational Codeforces Round 82
- Educational Codeforces Round 171 (Div. 2)
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)
- Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 93 (Rated for Div. 2)
- Educational Codeforces Round 96 A-E 題解
- Educational Codeforces Round 170 (Rated for Div. 2)
- Educational Codeforces Round 171 div2(A~E)
- Educational Codeforces Round 19 E. Array Queries
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)B
- Educational Codeforces Round 100-C. Busy Robot
- Educational Codeforces Round 100 (Rated for Div. 2)
- Educational Codeforces Round 33 (Rated for Div. 2) C
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Educational Codeforces Round 163 (Rated for Div. 2)
- Educational Codeforces Round 154 (Rated for Div. 2)
- Educational Codeforces Round 168 (Rated for Div. 2)
- Educational Codeforces Round 166 個人題解(A~D)
- Educational Codeforces Round 166(A-D題解)
- Educational Codeforces Round 93 (Rated for Div. 2)題解
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- Educational Codeforces Round 170 (Rated for Div. 2) ABCD
- Educational Codeforces Round 171 (Rated for Div. 2) 題解
- Educational Codeforces Round 168 (Rated for Div. 2) A - E
- 【CodeForces訓練記錄】Educational Codeforces Round 171 (Rated for Div. 2)
- Educational Codeforces Round 143 (Rated for Div. 2) A-E
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- vp Educational Codeforces Round 168 (Rated for Div. 2) A - E
- Educational Codeforces Round 168 (Rated for Div. 2) 題解
- Educational Codeforces Round 4 D The Union of k-Segments (掃描線)
- Educational Codeforces Round 95 (Rated for Div. 2) G. Three Occurrences
- Educational Codeforces Round 163 (Rated for Div. 2) - VP記錄
- Educational Codeforces Round 166 (Rated for Div. 2) - VP記錄
- Educational Codeforces Round 162 (Rated for Div. 2) - VP記錄
- Educational Codeforces Round 159 (Rated for Div. 2) - VP記錄