圓周率日挑戰
py挑戰
import decimal
import math
pi = decimal.Decimal('3.1415926535897932384626433832795028841971693993751'
'058209749445923078164062862089986280348253421170679')
n = int(input())
ans: decimal = pi
ans_a: int = 0
ans_b: int = 0
for i in range(n):
a, b = map(int, input().split())
c = abs(decimal.Decimal(str(a)) / decimal.Decimal(str(b)) - pi)
if c < ans:
ans = c
ans_a = a
ans_b = b
elif c == ans:
if ans_a > a:
ans = c
ans_a = a
ans_b = b
print(int(ans_a), int(ans_b))
正規表示式
簽到
void solve()
{
int t,a,b,c,d;
scanf("%d",&t);
int ans = t;
while(t--)
{
scanf("%d.%d.%d.%d",&a,&b,&c,&d);
//cout<<a<<b<<c<<d<<endl;
if(a<0||a>255||b<0||b>255||c<0||c>255||d<0||d>255) ans--;
}
cout<<ans<<endl;
}
Circle
找規律就行
void solve()
{
cin>>n;
if(n==0)
{
cout<<1<<" ";
return;
}
cout<<(n-1)*n+2<<" ";
}
開心消消樂(Right Version)
消消樂,和前面一樣的一起消
不一樣的自己單獨消
void solve()
{
int x = -1;
int ans = 0;
cin>>n;
rep(i,1,n) cin>>a[i];
rep(i,1,n)
{
if(a[i]!=x)
{
x=a[i];
if(a[i]) ans++;
}
}
cout<<ans<<endl;
}
區間
線段樹套板子,程式碼太長就不放了qaq
累加器
維護一個每一個數與前一個數的位數差的字首和
然後差分一下
int dif(int n,int m)
{
int t = n^m;
int count=0;
while(t)
{
t=t&(t-1);
count++;
}
return count;
}
int cal(int x)
{
return dif(x,x-1);
}
void solve()
{
int x,y;
cin>>x>>y;
cout<<a[x+y]-a[x]<<endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
a[0]=0;
for(int i=1;i<=4e6+10;i++)
{
a[i]=a[i-1]+cal(i);
}
int t=1;
cin >> t;
while(t--) {
solve();
}
return 0;
}
求值
把abc排序
先把ans設為一箇中間值
然後再一點點調整
ans大了 就刪中b放小c
ans小了 就刪中b放大c
void solve()
{
cin>>a>>b>>c>>n>>w;
if(a<b)
{
a^=b^=a^=b;
}
if(a<c)
{
a^=c^=a^=c;
}
if(b<c)
{
b^=c^=b^=c;
}
int tmp = n*b-w;
int ans = abs(tmp);
rep(i,1,n)
{
if(tmp>0)
{
tmp -= b;
tmp += c;
}
else if(tmp<0)
{
tmp -= b;
tmp += a;
}
else if(tmp==0)
{
ans = 0;
break;
}
ans = min(ans,abs(tmp));
}
cout<<ans<<endl;
return;
}
遊戲
跑兩個dij,一個由1到n,一個1到k+k到n,看哪個短
void solve()
{
cin>>n>>m>>k;
vector<vector<pii>> v1(n+1), v2(n+1);
for(int i=1;i<=m;i++)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
v2[a].pb({b,c});
v2[b].pb({a,c});
if(d)
{
v1[a].pb({b,c});
v1[b].pb({a,c});
}
}
auto dijkstra = [&](int root, int to, vector<vector<pii>> e) -> int{
vector<int> d(n+1, 1e18);
vector<int> vis(n+1);
priority_queue<pii, vector<pii>, greater<pii>> minHeap;
d[root] = 0, vis[root] = 0, minHeap.push({0, root});
while(!minHeap.empty()){
int distance = minHeap.top().first, now = minHeap.top().second;
minHeap.pop();
if(vis[now]) continue;
vis[now] = true;
for(auto it : e[now]){
int son = it.first, w = it.second;
if(d[son] > distance + w){
d[son] = distance + w;
minHeap.push({d[son], son});
}
}
}
return d[to];
};
int ans1 = dijkstra(1,n,v1);
int ans2 = dijkstra(1,k,v1)+dijkstra(k,n,v2);
int ans = min(ans1,ans2);
if(ans >= 1e18){
cout<<-1<<endl;
return;
}else{
cout<<ans<<endl;
return;
}
}
keillempkill學姐の卷積
模擬
void solve()
{
cin>>n>>m;
rep(i,1,n)
{
rep(j,1,n)
{
cin>>a[i][j];
}
}
rep(i,1,m)
{
rep(j,1,m)
{
cin>>b[i][j];
}
}
for(int i=1;i<=m-n+1;i++)
{
for(int j=1;j<=m-n+1;j++)
{
for(int u=1;u<=n;u++)
{
for(int w=1;w<=n;w++)
{
c[i][j]+=b[i+u-1][j+w-1]*a[u][w];
}
}
}
}
rep(i,1,m-n+1)
{
rep(j,1,m-n+1)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
}
暴食之史萊姆
即使旁邊的史萊姆比當前史萊姆大
也可以透過讓旁邊史萊姆吃其他史萊姆的方式 縮小旁邊的史萊姆
所以可以分為左右兩側考慮
用單調棧維護一個終點為當前史萊姆的遞增子序列
cin>>n;
int tt = 0;
rep(i,1,n) cin>>a[i];
rep(i,1,n)
{
while(tt&&a[stk[tt]]>a[i]) tt--;
stk[++tt] = i;
b[i] = tt-1;
}
tt = 0;
for(int i=n;i>=1;i--)
{
while(tt&&a[stk[tt]]>a[i]) tt--;
stk[++tt] = i;
c[i] = tt-1;
}
rep(i,1,n)
{
cout<<b[i]+c[i]<<" ";
}
SSH
模擬
void solve()
{
int m,n,q;
cin>>m>>n>>q;
for(int i=0;i<m;i++){
cin>>pub>>pri;
p[pri]=pub;
}
for(int i=0;i<n;i++){
cin>>ip>>k;
while(k--){
cin>>user>>t;
while(t--){
cin>>s;
u[ip][user][s]=1;
}
}
}
for(int i=0;i<q;i++){
cin>>user>>ip>>pri;
if(u[ip][user][p[pri]]==1){
cout<<"Yes\n";
}else cout<<"No\n";
}
}