# 【URAL水题】集中题解区

705人阅读 评论(0)

http://acm.timus.ru/problem.aspx?space=1&num=1346

/*		感觉有规律，但是没法证明。。	*\
\* 		可能不完美。对付URAL的测试足够了*/
#include <cstdio>

long a;long b;
long num[100010];
long n = 0;
int main()
{
scanf("%ld%ld",&a,&b);
b -= a-1;
a = 1;
long last = 0;
for (long i=1;i<b+1;i++)
{
scanf("%ld",num+i);
if (num[i]!=last)
{
last = num[i];
num[++n] = num[i];
}
}
long cnt = 1;
long inc = num[2]>num[1]?1:-1;
long continus = 0;
for (long i=2;i<n+1;i++)
{
long tmp = num[i]>num[i-1]?1:-1;
if (tmp != inc)
{
inc = -inc;
cnt ++;
continus++;
}
else
{
continus = 0;
}

/*								*\
连续两个拐点，则从中间断开
如果连续三个拐点，只能断开其中之一
\*								*/

if (continus == 2)
{
cnt --;
continus = 0;
}
}
printf("%ld",cnt);
return 0;
}

http://acm.timus.ru/problem.aspx?space=1&num=1167

#include <cstdio>
#include <iostream>

long n;long K;
long col[510];
long sum[510];
long f[510][510];

//long BLACK(long a,long b){return (sum[b]-sum[a-1]);}
//long WHITE(long a,long b){return ((b-a+1)-(sum[b]-sum[a-1]));}

#define BLACK(a,b) (sum[b]-sum[a-1])
#define WHITE(a,b) (((b)-(a)+1)-(sum[(b)]-sum[(a)-1]))
/*							*\

\*							*/

#define MIN(a,b) (a<b?a:b)

int main()
{
scanf("%ld%ld",&n,&K);
for (long i=1;i<n+1;i++)
{
scanf("%ld",col+i);
sum[i] = sum[i-1] + col[i];
}
for (long i=0;i<n+1;i++)
for (long j=0;j<K+1;j++)
f[i][j] = 0x7f7f7f7f;
f[0][0] = 0;
for (long i=1;i<n+1;i++)
{
#ifdef Debug
std::cerr << i << std::endl;
#endif
for (long j=1;j<K+1&&j<i+1;j++)
{
f[i][j] = 0x7f7f7f7f;
for (long k=0;k<i;k++)
{
f[i][j] = MIN(f[i][j],f[k][j-1]+BLACK(k+1,i)*WHITE(k+1,i));
}
//			f[i][j] = MIN(f[i][j],f[i-1][j]);
}
}
printf("%ld",f[n][K]);
return 0;
}


http://acm.timus.ru/problem.aspx?space=1&num=1078

#include <cstdio>
#include <algorithm>

long n;
const long inf = 0x7f7f7f7f;
long g[510];

struct node
{
long l;
long r;
long i;
bool operator<(const node& n2)const
{
if (l < n2.l) return true;
if (l > n2.l) return false;
if (r < n2.r) return false;
return true;
}
};
node line[510];
long f[510];
long output[510];
long top = 0;

#define MAX(a,b) (a>b?a:b)

int main()
{
scanf("%ld",&n);
for (long i=1;i<n+1;i++)
{
scanf("%ld%ld",&line[i].l,&line[i].r);
line[i].i = i;
}
std::sort(line+1,line+n+1);
line[0].l = -inf;
line[0].r = inf;
long ans = 0;
long des = 0;
for (long i=1;i<n+1;i++)
{
for (long j=0;j<i;j++)
{
if (line[j].r > line[i].r && line[j].l < line[i].l)
{
if (f[j] + 1 > f[i])
{
f[i] = f[j] + 1;
g[i] = j;
}
}
}
if (f[i] > ans)
{
ans = f[i];
des = i;
}
}
printf("%ld\n",ans);
long i = des;
while (i)
{
output[++top] = line[i].i;
i = g[i];
}
for (long i=1;i<top+1;i++)
{
printf("%ld ",output[i]);
}
return 0;
}


http://acm.timus.ru/problem.aspx?space=1&num=1157

#include <cstdio>
#include <cmath>

long m;long n;long k;
long cnt = 0;

long ans(long x)
{
long t = 0;
//	for (long i=1;i<=ceil(sqrt(double(x)));i++)
for (long i=1;i<=long(sqrt(double(x))+0.5);i++)
{
if (x % i == 0) t++;
}
return t;
}

int main()
{
scanf("%ld%ld%ld",&m,&n,&k);
for (long i=k+1;i<10001;i++)
{
if (ans(i)==n&&ans(i-k)==m)
{
printf("%ld\n",i);
return 0;
}
}
printf("0\n");
return 0;
}


http://acm.timus.ru/problem.aspx?space=1&num=1029

#include <cstdio>
typedef long long ll;

long m;long n;

ll f1[510];
ll f2[510];
long g1[510];
long g2[510];
long g[110][510];
ll f[110][510];
long map[110][510];
long op[50010];
long top = 0;

int main()
{
scanf("%ld%ld",&n,&m);
for (long i=1;i<n+1;i++)
{
for (long j=1;j<m+1;j++)
{
scanf("%ld",map[i]+j);
}
}
for (long i=1;i<n+1;i++)
{
f1[0] = f1[m+1] =
f2[0] = f2[m+1] = 0x3f7f7f7f7f7f7f7fll;
for (long j=1;j<m+1;j++)
if (f[i-1][j]+(ll)map[i][j] < f1[j-1]+(ll)map[i][j])
{
f1[j] = f[i-1][j]+(ll)map[i][j];
g1[j] = j;
}
else
{
f1[j] = f1[j-1]+(ll)map[i][j];
g1[j] = j-1;
}

for (long j=m;j>0;j--)
if (f[i-1][j]+(ll)map[i][j] < f2[j+1]+(ll)map[i][j])
{
f2[j] = f[i-1][j]+(ll)map[i][j];
g2[j] = j;
}
else
{
f2[j] = f2[j+1]+(ll)map[i][j];
g2[j] = j+1;
}
for (long j=1;j<m+1;j++)
{
if (f1[j] < f2[j])
{
f[i][j] = f1[j];
g[i][j] = g1[j];
}
else
{
f[i][j] = f2[j];
g[i][j] = g2[j];
}
}
}
ll ans = 0x7f7f7f7f7f7f7f7fll;
long des = 0;
for (long j=1;j<m+1;j++)
{
if (ans > f[n][j])
{
ans = f[n][j];
des = j;
}
}
long i = n;
long j = des;
while (g[i][j])
{
if (j == g[i][j])
{
op[++top] = j;
i --;
}
else
{
op[++top] = j;
j = g[i][j];
}
}
for (long i=top;i>0;i--)
{
printf("%ld ",op[i]);
}
//printf("%ld",ans);
return 0;
}


http://acm.timus.ru/problem.aspx?space=1&num=1002

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
using std::min;
using std::string;
using std::cout;
using std::cin;
string PN;
string wrds[50010];
string wrds2[50010];
char chrctr[26] = {'2','2','2','3','3','3','4','4','1','1','5','5','6','6','0','7','0','7','7','8','8','8','9','9','9','0'};
long length[50010];
long f[50010];
long g[50010];
long pnlen;
long n;

void dp()
{
for (long i=0;i<pnlen;i++)
{
g[i] = 0;
f[i] = 0x7f7f7f7f;
}
for (long i=1;i<n+1;i++)
{
if (PN.compare(0,length[i],wrds[i],0,length[i])==0)
{
f[length[i]-1] = 1;
g[length[i]-1] = i;
}
}
for (long i=1;i<pnlen;i++)
{
for (long j=1;j<n+1;j++)
{
if (i-length[j]>=0 && PN.compare(i-length[j]+1,length[j],wrds[j],0,length[j])==0)
{
if (f[i-length[j]]+1 < f[i])
{
f[i] = f[i-length[j]]+1;
g[i] = j;
}
}
}
}
}

void ou(long l)
{
if (g[l] == 0)return;
ou(l-length[g[l]]);
cout << wrds2[g[l]] << " ";
}

void translate(long l)
{
for (long i=0;i<length[l];i++)
{
wrds[l][i] = chrctr[wrds[l][i]-'a'];
}
}

int main()
{
freopen("phone.in","r",stdin);
freopen("phone.out","w",stdout);
while (1)
{
cin >> PN;
if (PN == "-1") break;
pnlen = PN.length();
cin >> n;

for (long i=1;i<n+1;++i)
{
cin >> wrds[i];
wrds2[i] = wrds[i];
length[i] = wrds[i].length();
translate(i);
}
dp();
if (f[pnlen-1]<0x7f7f7f7f) ou(pnlen-1);
else cout << "No solution.\n";
cout << "\n";
}
}


0
0

【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐（算法+实战）--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：326453次
• 积分：7186
• 等级：
• 排名：第3089名
• 原创：419篇
• 转载：14篇
• 译文：0篇
• 评论：32条
文章分类
最新评论