九点开始做,11点半图书馆赶人,还有两题顺便去吃饭,下午OJ崩坏了,好了之后,终于Ak了。。。
水题。。。而且和大一一起训。。压力山大。
A - Hungry Sequence
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting of n integers.
A sequence a1, a2, ..., an, consisting of n integers, is Hungry if and only if:
- Its elements are in increasing order. That is an inequality ai < aj holds for any two indices i, j(i < j).
- For any two indices i and j(i < j), aj must not be divisible by ai.
Iahub is in trouble, so he asks you for help. Find a Hungry sequence with n elements.
Output
Output a line that contains n space-separated integers a1a2, ..., an(1 ≤ ai ≤ 107), representing a possible Hungry sequence. Note, that each ai must not be greater than 10000000 (107) and less than 1.
If there are multiple solutions you can output any one.
大水题,从10^6开始找n个数就好。。。
#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
for(int i=1;i<n;i++)
{
cout<<i+100000<<" ";
}
cout<<n+100000<<endl;
}
return 0;
}
B - Primes on Interval
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors.
Consider positive integers a, a + 1, ..., b (a ≤ b). You want to find the minimum integer l (1 ≤ l ≤ b - a + 1) such that for any integerx (a ≤ x ≤ b - l + 1) among l integers x, x + 1, ..., x + l - 1 there are at least k prime numbers.
Find and print the required minimum l. If no value l meets the described limitations, print -1.
Output
In a single line print a single integer — the required minimum l. If there's no solution, print -1.
/*
筛法求素+二分
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int prime[1000005];
int sum[1000005];
int a,b,k;
void init()
{
memset(prime,1,sizeof(prime));
prime[0]=0;
prime[1]=0;
for(int i=2;i<=1000000;i++)
{
if(prime[i])
{
for(int j=i+i;j<=1000000;j+=i)
{
prime[j]=0;
}
}
}
sum[0]=0;
sum[1]=0;
for(int i=2;i<=1000000;i++)
{
sum[i]=prime[i]?sum[i-1]+1:sum[i-1];
}
}
int ok(int len)
{
for(int i=a;i<=b-len+1;i++)
{
if(sum[i+len-1]-sum[i-1]<k)
{
return 0;
}
}
return 1;
}
int main()
{
init();
int flag=0;
cin>>a>>b>>k;
int left=1;
int right=b-a+1;
int mid;
while(left<=right)
{
mid=(left+right)/2;
if(ok(mid))
{
flag=1;
right=mid-1;
}
else
{
left=mid+1;
}
}
if(flag)
{
cout<<left<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
C - Increase and Decrease
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array ai, aj (i ≠ j);
- he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
Output
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
也是大水题,显然如果平均数存在(整数),一定能够得到n个相同的数,否则就是n-1个数
/*
平均数存在就好
*/
#include<iostream>
using namespace std;
int main()
{
int n;
int a[100005];
int sum=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
if(sum%n)
{
cout<<n-1<<endl;
}
else
{
cout<<n<<endl;
}
return 0;
}
D - Sum of Consecutive Prime Numbers
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
Output
The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.
/*
筛法建立素数表
然后暴搜
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int k;
int prime[10005];
int PRIME[10005];
void init()
{
memset(prime,1,sizeof(prime));
prime[0]=0;
prime[1]=0;
for(int i=2;i<=10000;i++)
{
if(prime[i])
{
PRIME[k++]=i;
for(int j=i+i;j<=10000;j+=i)
{
prime[j]=0;
}
}
}
}
int main()
{
init();
int n;
while(cin>>n&&n!=0)
{
int ans=0;
for(int i=0;PRIME[i]<=n;i++)
{
int cnt=0;
for(int j=i;j<k&&cnt<n;j++)
{
cnt+=PRIME[j];
}
if(cnt==n)
{
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
E - Yogurt factory
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10,000) weeks, the price of milk and labor will fluctuate weekly such that it will cost the company C_i (1 <= C_i <= 5,000) cents to produce one unit of yogurt in week i. Yucky's factory, being well-designed, can produce arbitrarily many units of yogurt each week.
Yucky Yogurt owns a warehouse that can store unused yogurt at a constant fee of S (1 <= S <= 100) cents per unit of yogurt per week. Fortuitously, yogurt does not spoil. Yucky Yogurt's warehouse is enormous, so it can hold arbitrarily many units of yogurt.
Yucky wants to find a way to make weekly deliveries of Y_i (0 <= Y_i <= 10,000) units of yogurt to its clientele (Y_i is the delivery quantity in week i). Help Yucky minimize its costs over the entire N-week period. Yogurt produced in week i, as well as any yogurt already in storage, can be used to meet Yucky's demand for that week.
Output
* Line 1: Line 1 contains a single integer: the minimum total cost to satisfy the yogurt schedule. Note that the total might be too large for a 32-bit integer.
Hint
OUTPUT DETAILS:
In week 1, produce 200 units of yogurt and deliver all of it. In week 2, produce 700 units: deliver 400 units while storing 300 units. In week 3, deliver the 300 units that were stored. In week 4, produce and deliver 500 units.
/*
简单贪心
当生产费用大于上一次的生产+储存时选择上次
同时更新上次
注意要用long long
*/
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
long long n,s;
cin>>n>>s;
long long last=INF;
long long ans=0;
for(int i=0;i<n;i++)
{
long long c,y;
cin>>c>>y;
if(c>last+s)
{
c=last+s;
last=c;
}
else
{
last=c;
}
ans+=c*y;
}
cout<<ans<<endl;
return 0;
}
F - Apple Catching
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.
Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).
Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
Output
* Line 1: The maximum number of apples Bessie can catch without walking more than W times.
Hint
INPUT DETAILS:
Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.
OUTPUT DETAILS:
Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
/*
dp
dp[i][j]表示第i分钟共移动j次最多收获的苹果树
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])
同时需要根据第i分钟哪棵树落苹果做相应变化
*/
#include<iostream>
#include<algorithm>
using namespace std;
int a[1005];
int dp[1005][35];
int main()
{
int t,w;
cin>>t>>w;
for(int i=1;i<=t;i++)
{
cin>>a[i];
}
//根据第一颗不同初始化不同
if(a[1]==1)
{
dp[1][0]=1;
dp[1][1]=0;
}
else
{
dp[1][0]=0;
dp[1][1]=1;
}
for(int i=1;i<=t;i++)
{
if(a[i]==1)
{
dp[i][0]=dp[i-1][0]+1;
}
else
{
dp[i][0]=dp[i-1][0];
}
for(int j=1;j<=w;j++)
{
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
if((j%2&&a[i]==2)||(j%2==0&&a[i]==1))
{
dp[i][j]++;
}
}
}
int ans=0;
for(int i=0;i<=w;i++)
{
ans=max(ans,dp[t][i]);
}
cout<<ans<<endl;
return 0;
}
G - Agri-Net
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
/*
最小生成树裸题
Prim
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
int map[105][105];
int visit[105];
int dis[105];
int n;
void prim()
{
memset(visit,0,sizeof(visit));
for(int i=1;i<=n;i++)
{
dis[i]=map[1][i];
}
visit[1]=1;
int ans=0;
for(int i=1;i<=n;i++)
{
int mark=1;
int mindist=INF;
for(int j=1;j<=n;j++)
{
if(!visit[j]&&dis[j]<mindist)
{
mindist=dis[j];
mark=j;
}
}
visit[mark]=1;
if(mindist==INF)//最小生成树注意跳出
{
break;
}
ans+=mindist;
for(int j=1;j<=n;j++)
{
if(!visit[j])
{
dis[j]=min(dis[j],map[mark][j]);//只有这里不一样
}
}
}
cout<<ans<<endl;
}
int main()
{
while(cin>>n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>map[i][j];
}
}
prim();
}
return 0;
}
H - Silver Cow Party
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Output
Line 1: One integer: the maximum of time any one cow must walk.
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
/*
正反两次spfa
做比赛代码写的粗糙了点
回头总结时重写一遍
*/
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,s;
int map[1005][1005];
int map2[1005][1005];
int d[1005];
int d2[1005];
int flag[1005];
void getmap()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
map[i][j]=i==j?0:INF;
map2[i][j]=map[i][j];
}
}
for(int i=0;i<m;i++)
{
int x,y,len;
cin>>x>>y>>len;
map[x][y]=min(map[x][y],len);
map2[y][x]=map[x][y];
}
}
queue<int>q;
void SPFA()
{
int i,x;
while(!q.empty())
{
q.pop();
}
memset(flag,0,sizeof(flag));
for(i=1;i<=n;i++)
d[i]=INF;
d[s]=0;
q.push(s);
flag[s]=1;
while(!q.empty())
{
x=q.front();//出队列
q.pop();
flag[x]=0;
for(i=1;i<=n;i++)
{
if(d[i]>d[x]+map[x][i])
{
d[i]=d[x]+map[x][i];
if(!flag[i])
{
q.push(i);//入队列
flag[i]=1;
}
}
}
}
}
void SPFA2()
{
int i,x;
while(!q.empty())
{
q.pop();
}
memset(flag,0,sizeof(flag));
for(i=1;i<=n;i++)
d2[i]=INF;
d2[s]=0;
q.push(s);
flag[s]=1;
while(!q.empty())
{
x=q.front();//出队列
q.pop();
flag[x]=0;
for(i=1;i<=n;i++)
{
if(d2[i]>d2[x]+map2[x][i])
{
d2[i]=d2[x]+map2[x][i];
if(!flag[i])
{
q.push(i);//入队列
flag[i]=1;
}
}
}
}
}
int main()
{
cin>>n>>m>>s;
getmap();
SPFA();
SPFA2();
int ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,d[i]+d2[i]);
}
cout<<ans<<endl;
return 0;
}
I - Raising Modulo Numbers
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Description
People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow:
Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions Ai
Bi from all players including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers.
You should write a program that calculates the result and is able to find out who won the game.
Output
For each assingnement there is the only one line of output. On this line, there is a number, the result of expression
(A1B1+A2B2+ ... +AHBH)mod M.
/*
快速幂模版
*/
#include<iostream>
using namespace std;
int ModExp(long long a,long long b,long n)
{
long long result=1;
long long y=a;
while(b)
{
if(b%2)
{
result=result*y%n;
}
y=y*y%n;
b/=2;
}
return result;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int MOD;
int n;
cin>>MOD;
cin>>n;
long long ans=0;
while(n--)
{
int a,b;
cin>>a>>b;
ans+=ModExp(a,b,MOD);
ans%=MOD;
}
cout<<ans%MOD<<endl;
}
return 0;
}
J - Sumsets
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description

Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.
Output
For each S, a single line containing d, or a single line containing "no solution".
/*
稀里糊涂就过了?
*/
#include<iostream>
#include<algorithm>
using namespace std;
int a[1005];
int n;
/*
int find(int left,int right,int need)
{
while(left<=right)
{
int mid=(left+right)/2;
if(a[mid]==need)
{
return 1;
}
else if(a[mid]<need)
{
left=mid+1;
}
else
{
right=mid-1;
}
}
return 0;
}
*/
int main()
{
while(cin>>n&&n!=0)
{
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
int flag=0;
int ans=0;
for(int i=n;i>=1;i--)
{
if(flag)
{
break;
}
for(int j=n;j>=1;j--)
{
if(flag)
{
break;
}
if(i==j)
{
continue;
}
int key=a[i]-a[j];
for(int left=1,right=j-1;left<right; )
{
if(a[left]+a[right]==key)
{
flag=1;
ans=a[i];
break;
}
else if(a[left]+a[right]>key)
{
right--;
}
else
{
left++;
}
}
}
}
if(flag)
{
cout<<ans<<endl;
}
else
{
cout<<"no solution"<<endl;
}
}
return 0;
}
K - Find them, Catch them
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Output
For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."
Sample Output
Not sure yet.
In different gangs.
In the same gang.
/*
并查集扩展
其中mark[x]=0表示x与father[x]同
mark[x]=1表示x与father[x]不同
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int father[100005];
int mark[100005];
int n;
void init()
{
for(int i=1;i<=n;i++)
{
father[i]=i;
mark[i]=0;
}
}
int find(int x)
{
if(x==father[x])
{
return x;
}
else
{
int temp=find(father[x]);
if(mark[father[x]]==mark[x])
{
mark[x]=0;
}
else
{
mark[x]=1;
}
father[x]=temp;
return temp;
}
}
void Union(int x,int y)
{
int xx=find(x);
int yy=find(y);
father[xx]=yy;
if(mark[y]==0)
{
mark[xx]=1-mark[x];
}
else
{
mark[xx]=mark[x];
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m;
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++)
{
char str[10];
int a,b;
scanf("%s%d%d",str,&a,&b);
if(str[0]=='D')
{
Union(a,b);
}
else
{
int xx=find(a);
int yy=find(b);
if(xx!=yy)
{
printf("Not sure yet.\n");
}
else if(mark[a]==mark[b])
{
printf("In the same gang.\n");
}
else
{
printf("In different gangs.\n");
}
}
}
}
return 0;
}
L - Monthly Expense
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
/*
就是二分
应该是AK了吧。。
*/
#include<iostream>
using namespace std;
int a[100005];
int main()
{
int n,m;
cin>>n>>m;
int left=0;
int right=0;
int mid;
for(int i=0;i<n;i++)
{
cin>>a[i];
right+=a[i];
left=max(left,a[i]);
}
while(left<=right)
{
int ans=0;
int sum=0;
int mid=(left+right)/2;
for(int i=0;i<n;i++)
{
sum+=a[i];
if(sum>mid)
{
ans++;
sum=a[i];
}
}
if(ans<m)
{
right=mid-1;
}
else
{
left=mid+1;
}
}
cout<<left<<endl;
return 0;
}
大概都是算法的基本应用,而且今早八点到九点这段时间自己恰好在联系最短路和最小生成树模版。。于是好几道裸模版的题就直接上了
终于跟上训练的脚步了。。。
加油!