Description

Problem E
Open Credit System
Input: Standard Input
Output: Standard Output
In an open credit system, the students can choose any course they like, but there is a problem. Some of the students are more senior than other students. The professor of such a course has found quite a number of such students who came from senior classes (as if they came to attend the pre requisite course after passing an advanced course). But he wants to do justice to the new students. So, he is going to take a placement test (basically an IQ test) to assess the level of difference among the students. He wants to know the maximum amount of score that a senior student gets more than any junior student. For example, if a senior student gets 80 and a junior student gets 70, then this amount is 10. Be careful that we don't want the absolute value. Help the professor to figure out a solution.
Input
Input consists of a number of test cases T (less than 20). Each case starts with an integer n which is the number of students in the course. This value can be as large as 100,000 and as low as 2. Next n lines contain n integers where the i'th integer is the score of the i'th student. All these integers have absolute values less than 150000. If i < j, then i'th student is senior to the j'th student.
Output
For each test case, output the desired number in a new line. Follow the format shown in sample input-output section.
Sample Input Output for Sample Input
3 2 100 20 4 4 3 2 1 4 1 2 3 4 | 80 |
Problemsetter: Mohammad Sajjad Hossain
Special Thanks: Shahriar Manzoor
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
int ans=-INF;
int now;
int last=-INF;
cin>>n;
while(n--)
{
cin>>now;
ans=max(ans,last-now);
last=max(last,now);
}
cout<<ans<<endl;
}
// system("pause");
return 0;
}
Description

B | Age Sort Input: Standard Input Output: Standard Output | |
You are given the ages (in years) of all people of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.
Input
There are multiple test cases in the input file. Each case starts with an integer n (0<n<=2000000), the total number of people. In the next line, there are n integers indicating the ages. Input is terminated with a case where n = 0. This case should not be processed.
Output
For each case, print a line with n space separated integers. These integers are the ages of that country sorted in ascending order.
Warning: Input Data is pretty big (~ 25 MB) so use faster IO.
Sample Input Output for Sample Input
5 3 4 2 1 5 5 2 3 2 3 1 0 | 1 2 3 4 5 1 2 2 3 3 |
Note: The memory limit of this problem is 2 Megabyte Only.
Problem Setter: Mohammad Mahmudur Rahman
Special Thanks: Shahriar Manzoor
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int a[2000005];
int main()
{
int T;
int n;
while(scanf("%d",&n)!=EOF&&n!=0)
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
for(int i=0;i<n-1;i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[n-1]);
}
// system("pause");
return 0;
}
/*
//刘汝佳书上的,回去看
原来金币数为a1,a2,`````an.
最终的金币为这些数的平均值,设为A
xi表示i给i+1的金币个数
a1+xn-x1=A
a2+x1-x2=A
a3+x2-x3=A
a4+x3-x4=A
.......
an+x(n-1)-xn=A
利用后面n-1个等式,用x1表示 x2,x3,...xn
x2=x1-(A-a2)
x3=x1-(2*A-a2-a3);
....
结果就是求|x1|+|x1-b1|+|x1-b2|+...+|x1-b[n-1]|的最小值,取中位数
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int MAXN=1000005;
long long a[MAXN],b[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
long long sum=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
long long A=sum/n;
b[0]=0;
for(int i=1;i<n;i++)
{
b[i]=b[i-1]+A-a[i+1];
}
sort(b,b+n);
long long t=b[n/2];
long long ans=0;
for(int i=0;i<n;i++)
{
ans+=abs(t-b[i]);
}
printf("%lld\n",ans);
}
// system("pause");
return 0;
}
/*
还是刘汝佳书上的,也回去看
长l厘米的木棍上有n只蚂蚁,蚂蚁要么向左爬,要么向右爬,速度1cm/s。当两只蚂蚁碰面时,同时掉转方向。给出每只蚂蚁的初始朝向和位置,求t秒后每只蚂蚁位置。
蚂蚁相遇掉头后其实就是相穿而过。但是蚂蚁的编号就搞乱了,因此一开始要编号每只蚂蚁。着重理解好蚂蚁开始和结束的位置。。
*/
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 10005
struct str
{
int x , d , id;
bool operator < (const str& a) const
{
return x < a.x;
}
}ant[N] , run[N];
int n , T, ans , f[N] , t , L , ca;
int main()
{
int i , x , y; char c;
cin >> T;
while (T --)
{
cin >> L >> t >> n;
printf("Case #%d:\n", ++ ca);
for (i = 1 ; i <= n ;i ++)
{
scanf("%d %c\n",&x,&c);
y = c == 'L' ? -1 : 1;
ant[i] = (str) {x , y , i};
run[i] = (str) {x + t * y , y , i};
}
sort(ant + 1, ant + n + 1);
sort(run + 1, run + n + 1);
for (i = 1 ; i <= n ;i ++)
f[ant[i].id] = i;
for (i = 1 ; i < n ;i ++)
if (run[i].x == run[i + 1].x)
run[i].d = run[i + 1].d = 0;
for (i = 1 ; i <= n ;i ++)
{
x = f[i];
if (run[x].x < 0 || run[x].x > L)
printf("Fell off\n");
else
{
printf("%d ",run[x].x);
if (run[x].d == -1)
printf("L\n");
if (run[x].d == 0)
printf("Turning\n");
if (run[x].d == 1)
printf("R\n");
}
}
printf("\n");
}
return 0;
}
/*
大意是:分馅饼,注意,每人(包括自己)有且只有一块,即每人的饼不能是由几块组合而成。。。。。求最大的体积,因为高是1,其实就是求做大的面积。。。
设最大的面积为x(即每人拿到的)。。。。转化成方程:a[1]/x + a[2]/x + ..... + a[n]/x = y + 1
其中a[i]/x指各块馅饼最多能分给几个人。。。。由于n是变化的。。。。。所以不能直接像方程那样做。。。。。
而写出一个函数。。。。。。。。。。。注意代码中的函数中的if (count >= y + 1),为什么不是count == y + 1呢?
因为不一定能等于y+1(除非特例),而我们求的解也只是近似解而已。。。。。。。而我们希望他接近y + 1,所以后面成立的时候
l = mid 。。。。。。即x升,y+1降。。。。。。
还有一点就是EPS不能太小。。。。不然超时。。。。。他保留4位,我们就计算到6(4+2)位就可以了。。。。
别忘了计算面积。。。。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#define MIN 1e-6
using namespace std;
const double PI = acos(-1.0);
int n,f;
int r[10005];
int ok(double x)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
cnt+=int(r[i]*r[i]*PI/x);
}
if(cnt>=f)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>f;
f+=1;
double sum=0;
for(int i=1;i<=n;i++)
{
cin>>r[i];
sum+=r[i]*r[i]*PI;
}
double right=sum/f;
double left=0;
double mid;
while(right-left>MIN)
{
mid=(right+left)/2;
if(ok(mid))
{
left=mid;
}
else
{
right=mid;
}
}
mid=(left+right)/2;
printf("%.4lf\n",mid);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int num[100005],sum[100005];
int n,s;
int search(int i)
{
int left,right,mid;
left=0,right=i;
while(left<=right)
{
mid=(left+right)>>1;
if(sum[i]-sum[mid]>=s)
left=mid+1;
else
right=mid-1;
}
return i-right;
}
int solve()
{
int ans=INF;
for(int i=1;i<=n;i++)
{
if(sum[i]>=s)
ans=min(ans,search(i));
}
return ans==INF?0:ans;
}
int main()
{
while(cin>>n>>s)
{
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
cin>>num[i];
sum[i]=num[i]+sum[i-1];
}
cout<<solve()<<endl;
}
return 0;
}
/*
筛法都不会写了我太渣了。。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int map[505][505];
int prime[100005];
int n,m;
void init()
{
int k=1;
memset(prime,1,sizeof(prime));
prime[0]=0;
prime[1]=0;
for(int i=2;i<=100005;i++)
{
if(prime[i])
{
for(int j=i+i;j<=100005;j+=i)
{
prime[j]=0;
}
}
}
}
int find(int x)
{
int i=x+1;
for( ;!prime[i];i++)
{
}
return i-x;
}
int main()
{
init();
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int e;
cin>>e;
if(!prime[e])
{
map[i][j]=find(e);
}
else
{
map[i][j]=0;
}
}
}
int ans=INF;
for(int j=0;j<m;j++)
{
int tempans=0;
for(int i=0;i<n;i++)
{
tempans+=map[i][j];
}
ans=min(tempans,ans);
}
for(int i=0;i<n;i++)
{
int tempans=0;
for(int j=0;j<m;j++)
{
tempans+=map[i][j];
}
ans=min(tempans,ans);
}
cout<<ans<<endl;
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int cnt[905];
int visit[905];
int n,k;
int main()
{
while(cin>>n>>k)
{
memset(cnt,0,sizeof(cnt));
memset(visit,0,sizeof(visit));
for(int i=0;i<k;i++)
{
cin>>cnt[i];
visit[cnt[i]]=1;
}
int j=1;
for(int i=0;i<k;i++)
{
cout<<cnt[i]<<" ";
int sum=1;
for(; ;j++)
{
if(!visit[j])
{
cout<<j<<" ";
visit[j]=1;
sum++;
}
if(sum==n)
break;
}
cout<<endl;
}
}
return 0;
}
Description
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the i-th book.
Valera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it.
Print the maximum number of books Valera can read.
Input
The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.
Output
Print a single integer — the maximum number of books Valera can read.
Sample Input
4 5 3 1 2 1
3
3 3 2 2 3
1
#include<iostream>
using namespace std;
int book[100005];
int n,t;
int main()
{
while(cin>>n>>t)
{
for(int i=0;i<n;i++)
{
cin>>book[i];
}
int sum=0;
int first=0;
int books=0;
int ans=0;
for(int i=0;i<n;i++)
{
if(sum+book[i]>t)
{
i--;
sum-=book[first];
books--;
first++;
}
else
{
sum+=book[i];
books++;
ans=max(books,ans);
}
}
cout<<ans<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int n;
int work(int a,int b)
{
if ((a == 1 || a == 2 || a == 6 || a == 5) && (b == 1 || b == 2 || b == 6 || b == 5))
return 3;
if ((a == 1 || a == 3 || a == 6 || a == 4) && (b == 1 || b == 3 || b == 6 || b == 4))
return 2;
if ((a == 3 || a == 2 || a == 5 || a == 4) && (b == 3 || b == 2 || b == 5 || b == 4))
return 1;
}
int main()
{
int i,x,y,a,b;
cin>>n>>x;
for(i=1;i<=n;i++)
{
cin>>a>>b;
y=work(a,b);
if(y!=x&&y!=7-x)
break;
}
if(i<=n)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[300005];
int main()
{
cin>>n;
long long ans=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
ans+=abs(a[i]-i);
}
cout<<ans<<endl;
return 0;
}
/*
排序贪心
刘汝佳书上的
*/
#include<iostream>
#include<algorithm>
using namespace std;
int a[200005];
int b[200005];
int n,m;
int main()
{
while(cin>>n>>m,n!=0&&m!=0)
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<m;i++)
{
cin>>b[i];
}
sort(a,a+n);
sort(b,b+m);
int j=0;
int ans=0;
int i;
for(i=0;i<n;i++)
{
if(j>=m)
{
break;
}
while(j<m&&a[i]>b[j])
{
j++;
}
if(j>=m)
{
break;
}
ans+=b[j];
j++;
}
if(i<n)
{
cout<<"Loowater is doomed!"<<endl;
}
else
{
cout<<ans<<endl;
}
}
return 0;
}
先睡觉去了