目录
A. Tricky Sum
Solution:根据题意模拟即可
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
LL n;cin>>n;
LL st=1;
LL s=0;
while(st<=n){
s+=st;
st*=2;
}
cout<<(1+n)*n/2-s*2<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
int t;cin>>t;
while(t--) solve();
}
B. Queries on a String
Solution:根据题意模拟即可
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
string s;cin>>s;
int m;cin>>m;
int l,r,k;
for(int i=0;i<m;i++){
cin>>l>>r>>k;
int len=r-l+1,cnt=k%(len);
string t=s.substr(l-1,r-l+1);
//cout<<t<<'\n';
string x="";
for(int j=len-cnt;j<len;j++){
x+=t[j];
}
for(int j=0;j<len-cnt;j++) x+=t[j];
// cout<<x<<'\n';
for(int j=l;j<=r;j++){
s[j-1]=x[j-l];
}
}
cout<<s<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
solve();
}
C. Nearest vectors
Solution:计算几何题,使用c++自带函数atan2(),acos(),把每个向量的角度值存入pair数组,然后按角度大小排序,夹角最小值的两个向量一定是相邻的。
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
int n;cin>>n;
vector<pair<long double,int>>ans;
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
ans.push_back({atan2(y,x),i+1});
}
sort(ans.begin(),ans.end());
long double mx=100000,l,r;
for(int i=0;i<n;i++){
long double t=ans[(i+1)%n].first-ans[i].first;
if(t<0) t+=2*acos(-1);
if(t<mx){
mx=t;
r=ans[(i+1)%n].second,l=ans[i].second;
}
}
cout<<l<<" "<<r<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
solve();
}
D. Igor In the Museum
Solution:不难看出每个联通块内的点能看到的画的数量一点=定是一样的,因此我们对二维数组floodfill,对联通块进行标记即可。
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e3+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
int n,m,k;
char mp[N][N];
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
int vis[N][N];
map<int,int>mp1;
int cur=1;
void bfs(int x,int y,int cnt){
queue<PII>q;
q.push({x,y});
vis[x][y]=cur;
while(!q.empty()){
auto t=q.front();
q.pop();
int x1=t.first,y1=t.second;
for(int i=0;i<4;i++){
int nx=x1+dx[i],ny=dy[i]+y1;
if(nx>=0&&nx<n&&ny>=0&&ny<m&&!vis[nx][ny]){
if(mp[nx][ny]!='*'){
vis[nx][ny]=cur;
q.push({nx,ny});
}else{
cnt++;
}
}
}
}
//cout<<cnt<<'\n';
mp1[cur]=cnt;
}
void solve()
{
cin>>n>>m>>k;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>mp[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!vis[i][j]&&mp[i][j]=='.'){
bfs(i,j,0);
cur++;
}
}
}
int x,y;
for(int i=0;i<k;i++){
cin>>x>>y;
cout<<mp1[vis[x-1][y-1]]<<'\n';
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
solve();
}
E. Chocolate Bar
Solution:根据数据范围不难想到本题可以用记忆化搜索dp[i][j][k]定义为长为i宽为j,得到面积为k的最小代价。
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
int n,m,k;
int dp[35][35][35*35];
int dfs(int x,int y,int cnt){
if(cnt==x*y||cnt==0) return 0;
int &res=dp[x][y][cnt];
if(~res) return res;
if(~dp[y][x][cnt]) return res=dp[y][x][cnt];
res=inf;
for(int i=1;i<=x-1;i++){
int nw=cnt;
for(int j=0;j<=nw;j++){
res=min(res,dfs(i,y,j)+dfs(x-i,y,nw-j)+y*y);
}
}
for(int i=1;i<=y-1;i++){
int nw=cnt;
for(int j=0;j<=nw;j++){
res=min(res,dfs(x,i,j)+dfs(x,y-i,nw-j)+x*x);
}
}
dp[y][x][cnt]=res;
return res;
}
void solve()
{
cin>>n>>m>>k;
cout<<dfs(n,m,k)<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
int t;cin>>t;
memset(dp,-1,sizeof(dp));
while(t--) solve();
}