文章目錄
- Problem A. Jhadgre的C語言程序
- Problem B. Wpremig的AH之戰
- Problem C. Wpremig的三角形
- Problem D. Jhadgre的梯子
- Problem E. Jhadgre的合唱隊形
- Problem F. Jhadgre的傷心地
- Problem G. Wpremig的稱球問題
- Problem H. Jhadgre的回家之路
- Problem I. Jhadgre的小餅乾
- Problem J. Jhadgre爬樓梯
- Problem K. Jhadgre的π
- Problem L. Wpremig's Niuniu
- Problem M. Wpremig和Jhadgre的藏寶圖
Problem A. Jhadgre的C語言程序
注意是 helle
Problem B. Wpremig的AH之戰
巴什博弈,分三種情況討論。
m<=n時,此時先手一開始就能贏。
m>n且m%(n+1) == 0 時,此時先手必敗。
m>n且m%(n+1) != 0 時,此時先手只要取m % (n + 1)。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int m, n;
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
if (m % (n + 1) == 0)
puts("You are loser");
else if (n >= m)
{
for (int i = m; i < n; i ++)
printf("%d ", i);
printf("%d\n", n);
}
else
printf("%d\n", m % (n + 1));
}
return 0;
}
Problem C. Wpremig的三角形
將三角形剖分成寬爲d的矩形,從高到低排序,那麼我們肯定選擇前L/d個矩形放在區域內最優,那麼無限剖分矩形後,假設我們選擇的最後一個矩形高度是H,那麼對於高度大於H的三角形來說,肯定有一個屬於該三角形的高度爲H的矩形被選擇了。那麼對於所有高度大於H的三角形來說,高度爲H的部分肯定都放在區域內了,即交點的高度都是H。
結論:
1.最短邊放在x軸上;
2.放置在x軸上的邊確定時,最優解滿足所有三角形之間的交點在一條平行於x軸的直線上
有了上面的結論後,顯然,三角形越高越容易被選擇,所以三角形應該越高越好。
有了這個結論,二分交點高度就能很容易地解決這個問題
#include <cstdio>
#include <cmath>
using namespace std;
const double eps = 1e-8;
const int maxn = 100010;
int T, cas = 1, n;
double r, a[maxn], b[maxn], c[maxn];
double area[maxn], height[maxn];
double f(double h)
{
double ret = 0;
for (int i=1;i<=n;i++)
if (h < height[i]) ret += a[i] * (1.0 - h / height[i]);
return ret;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%lf", &n, &r);
for (int i=1;i<=n;i++) scanf("%lf%lf%lf", &a[i], &b[i], &c[i]);
for (int i=1;i<=n;i++)
{
double p = (a[i] + b[i] + c[i]) / 2;
area[i] = sqrt(p * (p-a[i]) * (p-b[i]) * (p-c[i]));
height[i] = area[i] * 2 / a[i];
}
double low = 0, high = 1000000, mid;
while ( fabs(high - low) > eps )
{
mid = (low + high) / 2;
if (f(mid) <= r) high = mid;
else low = mid;
}
double ans = 0, h = mid;
for (int i=1;i<=n;i++)
if (h < height[i]) ans += a[i] * (1.0 - h / height[i]) * (height[i] - h) / 2;
printf("%.3lf\n", ans + h * r);
}
return 0;
}
Problem D. Jhadgre的梯子
送的,注意數據範圍剛好比int大,不是INT_MAX
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,l;
cin>>n>>l;
long long ans = l;
for (int i = 0 ; i < n ; ++i) {
long long x;cin>>x;
if (x > ans) ans = x;
}
cout<<ans - l<<endl;
return 0;
}
Problem E. Jhadgre的合唱隊形
本來想讓樹套樹可以過的…然而題目時限和空間限制都比較尷尬,所以索性改成整體二分了。
先考慮如果只詢問一次,那麼假設詢問的名次是k,我們可以二分答案mid,每次check就是遍歷所有修改,建立樹狀數組,對於一個給[x,y]區間加入一個數z的操作,如果z>=mid,那就給[a,b]整個區間+1。最後查詢區間的和,即這個區間中>=mid的數的數量。如果這個數量>=k則向上二分並記錄答案,否則向下二分
我們可以發現最消耗時間的地方就是維護了,而維護的操作只和mid有關,跟具體的詢問無關。所以考慮對詢問進行處理,每次把所有需要“向上二分”的詢問(即[mid+1,r])和“向下二分”的詢問(即[l,mid])分別放進一個集合,當然同時也要將可能對它們造成影響的修改也跟隨詢問加入集合,然後分開遞歸繼續操作。
對於詢問,如果詢問區間的和大於等於詢問的名次則把這個詢問分到“向上二分”的集合中,否則分到“向下二分”的集合中。
對於修改,如果一個修改所加入的數<=mid,那麼對於已經認定答案在[mid+1,r]的詢問一定是沒有貢獻的,所以只需要加到“向下二分”的集合中;
否則那麼這次修改對於區間[l,mid]的詢問一定是有貢獻的。如果把答案在[l,mid]的詢問所求的名次都減去1,則這個修改也只會對“向上二分”的集合中的詢問有貢獻,所以將這次修改加入“向上二分”的集合中。
至於其中數的個數用BIT維護一下就好了,區間更新+區間查詢。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAXN 50005
typedef long long LL;
using namespace std;
int n,m,id[MAXN],t1[MAXN],t2[MAXN],T=0;
LL ans[MAXN];
LL read(){
LL x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';c=getchar();
}
return x*f;
}
struct Node1{
int opt,a,b;
LL c;
}O[MAXN];
struct BIT{
LL c[MAXN];int sign[MAXN];
int lowbit(int x){return x&-x;}
void add(int pos,int x){
while(pos<=n){
if(sign[pos]!=T)c[pos]=0,sign[pos]=T;
c[pos]+=x,pos+=lowbit(pos);
}
}
LL query(int pos){
LL res=0;
while(pos>0){
if(sign[pos]!=T)c[pos]=0,sign[pos]=T;
res+=c[pos],pos-=lowbit(pos);
}
return res;
}
}c1,c2;
void ADD(int a,int b){
c1.add(a,1),c1.add(b+1,-1);
c2.add(a,a),c2.add(b+1,-b-1);
}
LL QUERY(int a,int b){
LL res=0;
res+=(b+1)*c1.query(b)-c2.query(b);
res-=a*c1.query(a-1)-c2.query(a-1);
return res;
}
void solve(int l,int r,int ansl,int ansr){
if(l>r)return;
if(ansl==ansr){
for(int i=l;i<=r;i++)
if(O[id[i]].opt==2)ans[id[i]]=ansl;
return;
}
int mid=(ansl+ansr)>>1;
int j=1,k=1;
T++;
for(int i=l;i<=r;i++){
int idx=id[i];
if(O[idx].opt==1){
if(O[idx].c<=mid)t1[j++]=idx;
else t2[k++]=idx,ADD(O[idx].a,O[idx].b);
}
else{
LL res=QUERY(O[idx].a,O[idx].b);
if(res>=O[idx].c)t2[k++]=idx;
else t1[j++]=idx,O[idx].c-=res;
}
}
--j,--k;
for(int i=1;i<=j;i++)id[l+i-1]=t1[i];
for(int i=1;i<=k;i++)id[l+j+i-1]=t2[i];
solve(l,l+j-1,ansl,mid),solve(l+j,r,mid+1,ansr);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++){
O[i].opt=read();
O[i].a=read();
O[i].b=read();
O[i].c=read();
id[i]=i;
}
solve(1,m,-n,n);
for(int i=1;i<=m;i++)
if(O[i].opt==2)
printf("%d\n",ans[i]);
return 0;
}
Problem F. Jhadgre的傷心地
遞歸,小dp,隨便寫。
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100000];
int fun(int x){
if (x > n) return 0;
return min(fun(a[x]),fun(x + 5)) + 1;
}
int main()
{
scanf("%d",&n);
for (int i = 1 ; i <= n ; ++i)
scanf("%d",&a[i]);
cout<< fun(1) * 2<<endl;
return 0;
}
Problem G. Wpremig的稱球問題
首先給出結論:若稱y次需要找出質量不同的球並且知道輕重,最多能稱的球數量是n =
證明思路如下,依次證明引理:
引理一:
如果有m個球或者是標準或者比標準重,以及n個球或者是標準或者比標準輕以及至少1個標準球,則k次稱量能夠確定是哪個球的充要條件是:
引理二:
如果有n個球不知道是標準重量還是更輕還是更重,以及至少1個標準球,則k次稱量能夠確定是哪個球的充要條件是:
引理三:
如果有n個球不知道是標準重量還是更輕還是更重,以及至少1個標準球,則k次稱量能夠確定是哪個球,以及這個球是更輕還是更重的充要條件是:
引理一的充分性證明思路是:
令。每邊放i個可能重的,k個可能輕的,然後j,r=0,1,2總共9種可能,分類討論一下可以遞歸解決。
必要性是顯然的。
引理二的充分性證明思路是:
個球,第一次個球放左邊,個球加上標準球放右邊,還餘下個第一次稱量結束,要麼平衡可以用遞歸證明,要麼不平衡用引理證明。
引理三的充分性證明思路同理,區別在於餘下個球。
引理三的必要性證明思路與上面那個問題二相同
引理二的比較性證明就麻煩一些,不過思路還是類似。如果第一次稱量不平衡要利用引理一,平衡的話遞歸。
最終兩個問題的證明都需要單獨討論第一次稱量,因爲沒有標準球可以借,並且數目比引理中少一,然後就可以利用引理的結論,通過類似的思路證明。
#include <bits/stdc++.h>
using namespace std;
int main(){
int x,y;
while (~scanf("%d%d",&x,&y)){
if (x <= 2){
puts("No");
continue;
}
long long now = 3;
int k;
for (k = 2 ; k < 15 ; ++k){
now *= 3;
if ((now-3)/2 >= x) break;
}
if (k <= y) puts("Yes");
else puts("No");
}
return 0;
}
Problem H. Jhadgre的回家之路
顯然如果按照"L"→"W"→"Q"的路徑很難進行搜索因爲要先到每個"W"再從"W"出發搜索到"Q"的最短路徑,但是這條路可以想象成"L"→"W"←"Q",這樣的話就不需要考慮"W"了。
所以兩遍bfs,分別從"L"爲起點和以"Q"爲起點,然後對每個"W"遍歷,從"L"和"Q"到這個點的最短路之和就是拿這把鑰匙回寢室的最短路,求個最短的就可以了。
或者兩遍bfs,分別從"L"和"Q"爲起點做兩次bfs,然後遍歷整個地圖對所有"W"判斷最短路
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 2010;
const int INF = 0x3f3f3f3f;
int n, m;
int vis[MAXN][MAXN];
int ret[2][MAXN][MAXN];
char a[MAXN][MAXN];
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
void bfs(int xxx, int yyy, int d) {
queue< pair<int, int> > q;
q.push(make_pair(xxx, yyy));
vis[xxx][yyy] = 1;
ret[d][xxx][yyy] = 0;
while(!q.empty()) {
int tx = q.front().first;
int ty = q.front().second;
q.pop();
for (int i = 0;i < 4; ++i) {
int x = tx + xx[i];
int y = ty + yy[i];
if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '#' && !vis[x][y]) {
ret[d][x][y] = ret[d][tx][ty] + 1;
vis[x][y] = 1;
q.push(make_pair(x, y));
}
}
}
}
int main() {
while (~scanf("%d %d", &n, &m)){
queue< pair<int, int> > q;
memset(ret, INF, sizeof(ret));
memset(vis, 0, sizeof(vis));
int sx, sy;
int tx, ty;
for (int i = 0;i < n; ++i) {
scanf("%s", a[i]);
for (int j = 0;j < m; ++j) {
if (a[i][j] == 'W') {
q.push(make_pair(i, j));
} else if (a[i][j] == 'L') {
sx = i;
sy = j;
} else if (a[i][j] == 'Q') {
tx = i;
ty = j;
}
}
}
bfs(sx, sy, 0);
memset(vis, 0, sizeof(vis));
bfs(tx, ty, 1);
int ans = INF;
while(!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
ans = min(ans, ret[0][x][y] + ret[1][x][y]);
}
printf("%d\n", ans);
}
return 0;
}
Problem I. Jhadgre的小餅乾
送的題目,string.find()即可
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;scanf("%d",&N);
int ans = 0 ;
while (N--){
string x;cin>>x;
if (x.find("zailaiyihe") != string::npos){
ans++;
}
}
cout<<ans<<endl;
return 0;
}
Problem J. Jhadgre爬樓梯
兩重for循環即可…注意取模不是是
#include <iostream>
#include <cstdio>
using namespace std;
const int mod = 100000007;
int dp[10100];
int main() {
int n;
dp[0] = 1;
for (int i = 1; i <= 10000; ++i) {
dp[i] = dp[i - 1];
for (int j = i - 2; j >= 0; j -= 2) {
dp[i] += dp[j];
dp[i] %= mod;
}
}
while (cin>>n)
cout << dp[n] << endl;
return 0;
}
Problem K. Jhadgre的π
BBP公式或者各種收斂方式都可以。
http://www.huanqiujiemi.com/7xqE25x.html
參考圖…日本發行的一本書
#include <cstdio>
#include <cmath>
#include <iostream>
#define MAX_C 56000
int a = 10000, b, c = MAX_C, d, e, f[MAX_C + 1], g, n, ans, cnt;
using namespace std;
int main()
{
int pp;
scanf("%d", &n);
a = 10000;
b = d = e = g = ans = cnt = 0;
c = MAX_C;
for (; b - c; ) f[b++] = a / 5;
for (; d = 0, g = c * 2; c -= 14, ans = e + d / a, e = d % a, cnt++) {
if (cnt * 4 > n) break;
for (b = c; d += f[b]*a, f[b] = d % --g, d /= g--, --b; d *= b);
}
if (n % 4 == 0) cout << (ans / 1000);
else if (n % 4 == 1) cout << ((ans / 100) % 10);
else if (n % 4 == 2) cout << ((ans / 10) % 10);
else if (n % 4 == 3) cout << (ans % 10);
cout<<endl;
return 0;
}
Problem L. Wpremig’s Niuniu
英語閱讀題
先用一個五維數組預處理出所有可能的得分,然後在求每一組數據的時候直接求和再/13即可。注意四捨五入。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9')c = ch, ch = getchar();
while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
if(c == '-')x = -x;
}
inline int min(int a,int b)
{return a<b?a:b;}
int ans[15][15][15][15][15];
int x,y,m,n,z,sum;
inline void init()
{
for(int a = 1;a <= 13;++ a)
for(int b = 1;b <= 13;++ b)
for(int c = 1;c <= 13;++ c)
for(int d = 1;d <= 13;++ d)
for(int e = 1;e <= 13;++ e)
{
if(a<5 && b<5 && c<5 && d<5 && e<5 && a+b+c+d+e <= 10)
ans[a][b][c][d][e] = 60;
else if(a>10 && b>10 && c>10 && d>10 && e>10)
ans[a][b][c][d][e] = 50;
else if((a==b && b==c && c==d) || (a==b && b==c && c==e) || (a==b && b==d && e==d) || (a==e && e==c && c==d) || (e==b && b==c && c==d))
ans[a][b][c][d][e] = 40;
else
{
x = min(a,10),y = min(b,10),m = min(c,10),n = min(d,10),z = min(e,10);
if((x+y+m)%10 == 0 || (y+m+n)%10 == 0 || (m+n+z)%10 == 0 || (x+y+n)%10 == 0 || (x+y+z)%10 == 0 || (y+m+z)%10 == 0 || (y+n+z)%10 == 0 || (x+n+z)%10 == 0 || (x+m+n)%10 == 0 || (x+m+z)%10 == 0)
{
sum = x+y+m+n+z;
if(sum%10 == 0)
ans[a][b][c][d][e] = 30;
else if(sum%10 < 7)
ans[a][b][c][d][e] = sum%10;
else
ans[a][b][c][d][e] = (sum%10)*2;
}
}
}
}
int main()
{
init();
int t;
read(t);
for(;t;--t)
{
sum = 0;
read(x),read(y),read(m),read(n);
for(register int i = 1;i <= 13;++ i)
sum += ans[x][y][m][n][i];
sum = (sum/13.0)+0.5;
printf("%d\n",sum);
}
return 0;
}
Problem M. Wpremig和Jhadgre的藏寶圖
對圖進行黑白染色
設黑色格子的數量爲 black
設白色格子的數量爲 white
設黑色格子的數值和爲 sumblack
設白色格子的數值和爲 sumwhite
設最後每個格子的數字都變爲x則
black * x – sumblack = white * x – sumwhite
x = (sumblack – sumwhite) / (black– white)
1.當black ≠ white時 可以解出 x 再用網絡流check
2.當black = white時 ,可以發現 對於一個合法的x, k>=x都是一個合法的解
因爲black = white => (black + white) % 2 == 0 可以構造一層的滿覆蓋
所以可以二分x 然後用網絡流check
對於check
建圖:
如果點k爲白,建邊(s, k, x – v[k])
如果點k爲黑,建邊(k, t, x – v[k])
對相鄰點u、v (u爲白),建邊 (u, v, inf)
判斷是否滿流。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
const int s=1690,t=1691;
const LL oo2=1e17,oo1=1e13,oo3=1e15;
int a[50][50],num[50][50],xx[]={0,0,1,-1},yy[]={1,-1,0,0},
fir[1700],ne[10010],to[10010],que[1700],f[1700],
m,n,tot;
LL w[10010];
bool ok(int x,int y)
{
return x>=1&&x<=n&&y>=1&&y<=m;
}
void init()
{
int i,j;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
scanf("%d",&a[i][j]);
}
void add(int u,int v,LL x)
{
tot++;
ne[tot*2]=fir[u];
fir[u]=tot*2;
to[tot*2]=v;
w[tot*2]=x;
ne[tot*2+1]=fir[v];
fir[v]=tot*2+1;
to[tot*2+1]=u;
w[tot*2+1]=0;
}
bool find()
{
int hd=1,tl=1,i,u,v;
que[1]=s;
memset(f,0,sizeof(f));
f[s]=1;
while (hd<=tl)
{
u=que[hd++];
for (i=fir[u];i;i=ne[i])
if (w[i]&&!f[v=to[i]])
{
f[v]=f[u]+1;
que[++tl]=v;
}
}
return f[t];
}
LL dfs(int u,LL lim)
{
int i,v;
LL ret=0,x;
if (u==t) return lim;
for (i=fir[u];i&&ret<lim;i=ne[i])
if (w[i]&&f[v=to[i]]==f[u]+1)
{
x=dfs(v,min(lim-ret,w[i]));
ret+=x;
w[i]-=x;
w[i^1]+=x;
}
if (!ret) f[u]=0;
return ret;
}
bool ok(LL x)
{
int i,j,kk,x1,y1;
tot=0;
memset(fir,0,sizeof(fir));
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (i+j&1) add(s,num[i][j],x-a[i][j]);
else add(num[i][j],t,x-a[i][j]);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (i+j&1)
for (kk=0;kk<4;kk++)
if (ok(x1=i+xx[kk],y1=j+yy[kk]))
add(num[i][j],num[x1][y1],oo3);
while (find())
{
int xxx;
xxx=1;
while (dfs(s,oo2));
}
for (i=fir[s];i;i=ne[i])
if (w[i]) return 0;
return 1;
}
void solve1()
{
int i,j;
LL s0=0,s1=0,x;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (i+j&1) s1+=a[i][j];
else s0+=a[i][j];
x=s0-s1;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (a[i][j]>x)
{
printf("-1\n");
return;
}
if (!ok(x)) printf("-1\n");
else printf("%lld\n",(x*m*n-s0-s1)/2);
}
void solve0()
{
int i,j;
LL s0=0,s1=0,x,l,r,mid;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (i+j&1) s1+=a[i][j];
else s0+=a[i][j];
if (s0!=s1)
{
printf("-1\n");
return;
}
l=0;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
l=max(l,(LL)a[i][j]);
r=oo1;
while (l<r)
{
mid=(l+r)/2;
if (ok(mid)) r=mid;
else l=mid+1;
}
if (l==oo1) printf("-1\n");
else printf("%lld\n",(l*m*n-s0-s1)/2);
}
int main()
{
int T,i,j;
for (i=1;i<=40;i++)
for (j=1;j<=40;j++)
num[i][j]=i*40+j;
scanf("%d",&T);
while (T--)
{
init();
if (m*n&1) solve1();
else solve0();
}
}