您的位置:首页 > 其它

2016长乐夏令营 Day3

2016-07-12 20:00 344 查看
T1:

     模拟。。模拟。。模拟。。GG,没有考虑到,文件在根目录的情况

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 3E4 + 10;

char check[30],Get[30],preans[maxn*26];
int len,Num,getlen,flag = 1;

void Judge(int prelen)
{
int le = strlen(Get);
if (le != len) return;
for (int i = 0; i < le; i++)
if (check[i] != Get[i])
return;
for (int i = 0; i < prelen; i++)
if (preans[i] != 0) printf("%c",preans[i]);
else {
char C = 92;
printf("%c",C);
}
for (int i = 0; i < len; i++) printf("%c",check[i]);
printf("\n");
}

void GetLine()
{
getlen = 0;
for (int i = 0; i < 30; i++) Get[i] = 0;
char c;
flag = 0;
while (scanf("%c",&c) != EOF) {
if (c == '\n') break;
flag = 1;
Get[getlen++] = c;
}
for (int i = 0; i < getlen; i++)
if (Get[i] == ' ' || Get[i] == '.') {
if (Get[i] == ' ') {
Num = 0;
for (int j = i+1; j < getlen; j++)
if ('0' <= Get[j] && Get[j] <= '9')
Num = Num*10 + Get[j] - '0';
else break;
}
getlen = i;
break;
}
}

void dfs(int tot,int pre)
{
Num = 0;
int Len = pre + getlen;
for (int i = pre; i < Len; i++)
preans[i] = Get[i-pre];
preans[Len++] = 0;
while (tot--) {
GetLine();
if (Num) dfs(Num,Len);
else Judge(Len);
}
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif

GetLine();
len = strlen(Get);
for (int i = 0; i < len; i++) check[i] = Get[i];
while (flag) {
GetLine();
if (!Num) Judge(0);
else break;
}
dfs(Num,0);
while (flag) {
GetLine();
Judge(0);
}
return 0;
}


 

 

T2:

    还是模拟。。。没考虑[]只有一个数的情况。。以及,辗转相除应该是除到某数为0时停止啊。。。。惨

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

typedef long long LL;

LL ans[110],a1,a2,a[3];
char ch[500];
int typ,flag = 1,len;

void GetLine()
{
char c;
flag = len = 0;
while (scanf("%c",&c) != EOF) {
flag = 1;
if (c == '\n') break;
ch[len++] = c;
}
if (ch[0] == '[') typ = 1; else typ = 2;
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("pure.in","r",stdin);
freopen("pure.out","w",stdout);
#endif

while (flag) {
GetLine();
if (typ == 1) {
if (!flag) break;
int cur = 0;
LL tot = 0;
for (int i = 1; i < len; i++) {
if ('0' <= ch[i] && ch[i] <= '9')
tot = tot*10LL + 1LL*(ch[i]-'0');
else ans[++cur] = tot,tot = 0;
}
if (cur == 1) {
printf("%I64d\n",ans[cur]);
continue;
}
a2 = ans[cur];
a1 = 1LL + a2*ans[cur-1];
for (int i = cur - 2; i > 0; i--) {
swap(a1,a2);
a1 += a2*ans[i];
}
printf("%I64d/%I64d\n",a1,a2);
}
else {
if (!flag) break;
LL tot; tot = a[0] = a[1] = 0;
int cur = 0;
for (int i = 0; i < len; i++) {
if ('0' <= ch[i] && ch[i] <= '9')
tot = tot*10LL + 1LL*(ch[i]-'0');
else a[++cur] = tot,tot = 0;
}
cur = 0; a1 = a[1]; a2 = tot;
for (; a2;) {
ans[++cur] = a1/a2;
a1 %= a2;
swap(a1,a2);
}
printf("[%I64d;",ans[1]);
for (int i = 2; i < cur; i++) printf("%I64d,",ans[i]);
printf("%I64d]\n",ans[cur]);
}
}
return 0;
}


 

T3:

    当 a[i] < a[j] - (j-i)时,j鄙视i(i < j)

    即 a[i] - i < a[j] - j 即 a[i] + (n-i) < a[j] + (n-j)

    把所有位置加上n-i求逆序对即可(归并排序。。。树状数组)

    但是。。苟弱用的是另一种奇妙的方法,多一个log,就不说了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 1E5 + 10;
const int mo = 12345;

int n,ans = 0,a[maxn],p[maxn],q[maxn],x[maxn],y[maxn];

bool cmp(const int &A,const int &B)
{
return p[A] < p[B];
}

bool cmp2(const int &A,const int &B)
{
return q[A] < q[B];
}

void binarysearch(int l,int r)
{
if (l == r) return;
int mid = (l+r) >> 1;
for (int i = l; i <= mid; i++) {
p[i] = a[i] + mid - i + 1;
x[i] = i;
}
sort(x + l,x + mid + 1,cmp);
for (int i = mid + 1; i <= r; i++) {
q[i] = a[i] - p[l] - (i - mid - 1);
y[i] = i;
}
sort(y + mid + 1,y + r + 1,cmp2);
int j = mid + 1;
bool flag = 1;
for (int i = l; i <= mid && flag; i++) {
int t = a[y[j]] - a[x[i]];
while (t <= y[j] - x[i]) {
++j;
if (j > r) {flag = 0; break;}
t = a[y[j]] - a[x[i]];
}
ans = (ans + (r-j+1)) % mo;
}
binarysearch(l,mid);
binarysearch(mid+1,r);
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("bis.in","r",stdin);
freopen("bis.out","w",stdout);
#endif

cin >> n;
for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
binarysearch(1,n);
cout << ans;
return 0;
}


T4:

    首先,树形dp求出每个点的最长链,然后,单调队列瞎搞之。。

    注意的是,一开始把lm,rm数组设成全局变量没控制好,导致各种冲突WAGG

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

typedef long long LL;
const int maxn = 1E6 + 10;

struct E{
LL to,w,from;
E(int _to = 0,int _w = 0,int _from = 0){to = _to; w = _w; from = _from;}
}edgs[maxn];

int cur,tot,n,m,tail[maxn],mi[maxn],ma[maxn];
LL f[maxn],p[maxn],d[maxn],lm[maxn],rm[maxn];

void dfs1(int x)
{
for (int i = tail[x]; i; i = edgs[i].from) {
int to = edgs[i].to;
dfs1(to);
d[x] = max(d[x],d[to] + edgs[i].w);
}
}

void dfs2(int x,LL from)
{
int L = ++tot,R = L - 1;
f[x] = max(from,d[x]);
int siz = 0;
for (int i = tail[x]; i; i = edgs[i].from) {
int to = edgs[i].to;
++R;
lm[R] = rm[R] = d[to] + edgs[i].w;
++siz;
}
tot = R;
for (int i = L; i < R; i++)
lm[i] = max(lm[i],lm[i-1]);
for (int i = R-1; i >= L; i--)
rm[i] = max(rm[i],rm[i+1]);
int po = L;
for (int i = tail[x]; i; i = edgs[i].from) {
LL maxx = from;
if (po > L) maxx = max(maxx,lm[po-1]);
if (po < R) maxx = max(maxx,rm[po+1]);
dfs2(edgs[i].to,maxx+edgs[i].w);
++po;
}
}

int getint()
{
int ret = 0;
char ch = getchar();
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9') ret = ret*10 + ch - '0',ch = getchar();
return ret;
}

void Add(int x,int y,int w)
{
edgs[++cur] = E(y,w,tail[x]);
tail[x] = cur;
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("race.in","r",stdin);
freopen("race.out","w",stdout);
#endif

cin >> n >> m;
for (int i = 2; i <= n; i++) {
int x,y;
x = getint(); y = getint();
Add(x,i,y);
}
dfs1(1);
dfs2(1,0);
int hi,ti,ha,ta,ans,head;
head = hi = ha = ans = 1;	ta = ti = 0;
mi[++ti] = 1; ma[++ta] = 1;
for (int j = 2; j <= n; j++) {
while (hi <= ti && f[mi[ti]] > f[j]) --ti;
while (ha <= ta && f[ma[ta]] < f[j]) --ta;
mi[++ti] = j; ma[++ta] = j;
while (f[ma[ha]] - f[mi[hi]] > m) {
++head;
while (ma[ha] < head) ++ha;
while (mi[hi] < head) ++hi;
}
ans = max(ans,j - head + 1);
//printf("%d\n",ans);
}
cout << ans;
//for (int i = 1; i <= n; i++) cout << f[i] << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: