题目1 : Visiting Peking University
-
7 3 6 9 10 1 0 8 35 3 5 6 2 4 2 10 11 1 2 1 2
样例输出
-
0 3 1 3
描述
Ming is going to travel for n days and the date of these days can be represented by n integers: 0, 1, 2, …, n-1. He plans to spend m consecutive days(2 ≤ m ≤ n)in Beijing. During these m days, he intends to use the first day and another day to visit Peking university. Before he made his plan, Ming investigated on the number of tourists who would be waiting in line to enter Peking university during his n-day trip, and the results could be represented by an integer sequence p[i] (0 ≤ i ≤ n-1, p[i] represents the number of waiting tourists on day i). To save time, he hopes to choose two certain dates a and b to visit PKU(0 ≤ a < b ≤ n-1), which makes p[a] + p[b] as small as possible.
Unfortunately, Ming comes to know that traffic control will be taking place in Beijing on some days during his n-day trip, and he won’t be able to visit any place in Beijing, including PKU, on a traffic control day. Ming loves Beijing and he wants to make sure that m days can be used to visit interesting places in Beijing. So Ming made a decision: spending k (m ≤ k ≤ n) consecutive days in Beijing is also acceptable if there are k - m traffic control days among those k days. Under this complicated situation, he doesn’t know how to make the best schedule. Please write a program to help Ming determine the best dates of the two days to visit Peking University. Data guarantees a unique solution.
输入
There are no more than 20 test cases.
For each test case:
The first line contains two integers, above mentioned n and m (2 ≤ n ≤ 100, 2 ≤ m ≤ n).
The second line contains n integers, above mentioned p[0] , p[1] , … p[n-1]. (0 ≤ p[i] ≤ 1000, i = 0 ... n-1)
The third line is an integer q (0 ≤ q ≤ n), representing the total number of traffic control days during the n-day trip, followed by q integers representing the dates of these days.
输出
One line, including two integers a and b, representing the best dates for visiting PKU.
要在北京待上连续m天,如果这m天中有k天的交通管制,那么这k天可以不算在m天里,即待上m+k天。
反之必须去玩m天,其中第一天和其他任意一天的p[i]加起来最小,求这两天的日期。
日期从0到n-1.
POINT:
简单模拟一下。
#include <iostream>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
#define LL long long
const int maxn = 111;
int p[maxn];
int flag[maxn];
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
memset(p,0,sizeof p);
memset(flag,0,sizeof flag);
for(int i=0;i<n;i++) scanf("%d",&p[i]);
int q;
scanf("%d",&q);
int a;
for(int i=1;i<=q;i++) scanf("%d",&a),flag[a]=1;
int ans1,ans2;
int minans=999999;
int k2;
for(int i=0;i<n;i++)
{
if(flag[i]) continue;
int cnt=0;
cnt++;
int minn=9999;
for(int j=i+1;j<n;j++)
{
if(flag[j]==0)
{
cnt++;
if(minn>p[j])
{
minn=p[j];
k2=j;
}
}
if(cnt==m)
{
if(minans>p[i]+minn)
{
minans=p[i]+minn;
ans1=i;
ans2=k2;
}
break;
}
}
}
printf("%d %d\n",ans1,ans2);
}
}
题目9 : Minimum
-
1 3 1 1 2 2 1 1 2 2 5 1 0 7 1 1 2 2 1 2 2 2 2 1 1 2
样例输出
-
1 1 4
描述
You are given a list of integers a0, a1, …, a2^k-1.
You need to support two types of queries:
1. Output Minx,y∈[l,r] {ax∙ay}.
2. Let ax=y.
输入
The first line is an integer T, indicating the number of test cases. (1≤T≤10).
For each test case:
The first line contains an integer k (0 ≤ k ≤ 17).
The following line contains 2k integers, a0, a1, …, a2^k-1 (-2k ≤ ai < 2k).
The next line contains a integer (1 ≤ Q < 2k), indicating the number of queries. Then next Q lines, each line is one of:
1. 1 l r: Output Minx,y∈[l,r]{ax∙ay}. (0 ≤ l ≤ r < 2k)
2. 2 x y: Let ax=y. (0 ≤ x < 2k, -2k ≤ y < 2k)
输出
For each query 1, output a line contains an integer, indicating the answer.
题意:
一些数,询问1,求l到r中任意数相乘最小,数可以重复。操作2,变一个数。
POINT:
线段树,记录区间里 正数的数量,负数的数量,绝对值最小的值,正数最大,负数最小
若区间内只有一种符号,那么答案就是绝对值最小的值^2。
若区间内有两种符号,那么答案就是正数最大*负数最小。
#include <iostream>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
#define LL long long
const LL maxn = 140000*4;
#define lt x<<1
#define rt x<<1|1
LL zmax[maxn],fumax[maxn];
LL Max[maxn];
LL znum[maxn],funum[maxn];
LL a[maxn/4];
void build(LL x,LL l,LL r)
{
if(l==r)
{
znum[x]=funum[x]=0;
zmax[x]=fumax[x]=0;
if(a[l]>=0) znum[x]++,zmax[x]=a[l];
else funum[x]++,fumax[x]=a[l];
Max[x]=abs(a[l]);
}
else
{
LL mid=(l+r)>>1;
build(lt,l,mid);
build(rt,mid+1,r);
funum[x]=funum[lt]+funum[rt];
znum[x]=znum[lt]+znum[rt];
zmax[x]=max(zmax[lt],zmax[rt]);
fumax[x]=min(fumax[lt],fumax[rt]);
Max[x]=min(Max[lt],Max[rt]);
}
}
LL numff,numzz,Maxx,zzmax,fufumax;
void ininit()
{
numff=0;
numzz=0;
Maxx=160000;
zzmax=0;
fufumax=0;
}
void query(LL x,LL l,LL r,LL ll,LL rr)
{
if(ll<=l&&rr>=r)
{
numff+=funum[x];
numzz+=znum[x];
Maxx=min(Maxx,Max[x]);
zzmax=max(zzmax,zmax[x]);
fufumax=min(fufumax,fumax[x]);
}
else
{
LL mid=(l+r)>>1;
if(ll<=mid)
query(lt,l,mid,ll,rr);
if(mid<rr)
query(rt,mid+1,r,ll,rr);
}
}
void change(LL x,LL l,LL r,LL p,LL now)
{
if(l==p&&r==p)
{
funum[x]=0,znum[x]=0;
zmax[x]=fumax[x]=0;
if(now>=0) znum[x]++,zmax[x]=now;
else funum[x]++,fumax[x]=now;
Max[x]=abs(now);
}
else
{
LL mid=(l+r)>>1;
if(p<=mid)
change(lt,l,mid, p, now);
else
change(rt,mid+1, r,p, now);
funum[x]=funum[lt]+funum[rt];
znum[x]=znum[lt]+znum[rt];
zmax[x]=max(zmax[lt],zmax[rt]);
fumax[x]=min(fumax[lt],fumax[rt]);
Max[x]=min(Max[lt],Max[rt]);
}
}
int main()
{
LL T;scanf("%lld",&T);
while(T--)
{
LL k;scanf("%lld",&k);
LL num=(LL)pow(2,k);
for(LL i=1;i<=num;i++)
{
scanf("%lld",&a[i]);
}
build(1,1,num);
LL q;scanf("%lld",&q);
while(q--)
{
LL kind;scanf("%lld",&kind);
if(kind==1)
{
LL l,r;
ininit();
scanf("%lld %lld",&l,&r);
query(1,1,num,l+1,r+1);
if(numff==0||numzz==0)
{
printf("%lld\n",Maxx*Maxx);
}
else
{
printf("%lld\n",zzmax*fufumax);
}
}
else
{
LL x,y;
scanf("%lld %lld",&x,&y);
change(1,1,num,x+1,y);
}
}
}
}