欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Codeforces Round #417 (Div. 2)总结

程序员文章站 2022-05-12 12:25:55
...
A. Sagheer and Crossroads
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sagheer is walking in the street when he comes to an intersection of two roads. Each road can be represented as two parts where each part has 3 lanes getting into the intersection (one for each direction) and 3 lanes getting out of the intersection, so we have 4 parts in total. Each part has 4 lights, one for each lane getting into the intersection (l — left, s — straight, r — right) and a light p for a pedestrian crossing.

Codeforces Round #417 (Div. 2)总结

An accident is possible if a car can hit a pedestrian. This can happen if the light of a pedestrian crossing of some part and the light of a lane that can get to or from that same part are green at the same time.

Now, Sagheer is monitoring the configuration of the traffic lights. Your task is to help him detect whether an accident is possible.

Input

The input consists of four lines with each line describing a road part given in a counter-clockwise order.

Each line contains four integers lsrp — for the left, straight, right and pedestrian lights, respectively. The possible values are 0 for red light and 1 for green light.

Output

On a single line, print "YES" if an accident is possible, and "NO" otherwise.

Examples
input
1 0 0 1
0 1 0 0
0 0 1 0
0 0 0 1
output
YES
input
0 1 1 0
1 0 1 0
1 1 0 0
0 0 0 1
output
NO
input
1 0 0 0
0 0 0 1
0 0 0 0
1 0 1 0
output
NO
Note

In the first example, some accidents are possible because cars of part 1 can hit pedestrians of parts 1 and 4. Also, cars of parts 2 and 3can hit pedestrians of part 4.

In the second example, no car can pass the pedestrian crossing of part 4 which is the only green pedestrian light. So, no accident can occur.

题意:一个红绿灯 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道
让你判断,汽车是否有可能撞到行人
注意 当前车道的左转有可能撞到别的车道的行人的
解:全特判
import java.util.Scanner;

/**
 * 作者:张宇翔 
 * 创建日期:2017年6月1日 上午11:16:36
 * 描述:ACM
 */

public class Main {

	private static final int Max = (int) (1e4 + 10);
	private static int l[];
	private static int s[];
	private static int r[];
	private static int p[];
	private static boolean ok1,ok2,ok3,ok4,ok;
	public static void main(String[] args) {
		InitData();
		GetAns();
	}

	private static void InitData() {
		Scanner cin = new Scanner(System.in);
		l=new int[10];
		s=new int[10];
		r=new int[10];
		p=new int[10];
		ok1=false;
		ok2=false;
		ok3=false;
		ok4=false;
		ok=false;
		for(int i=0;i<4;i++){
			l[i]=cin.nextInt();
			s[i]=cin.nextInt();
			r[i]=cin.nextInt();
			p[i]=cin.nextInt();
			if(i==0){
				if(l[i]==1){
					ok4=true;
					ok1=true;
				}
				if(s[i]==1){
					ok3=true;
					ok1=true;
				}
				if(r[i]==1){
					ok1=true;
					ok2=true;
				}
			}else if(i==1){
				if(l[i]==1){
					ok2=true;
					ok1=true;
				}
				if(s[i]==1){
					ok2=true;
					ok4=true;
				}
				if(r[i]==1){
					ok3=true;
					ok2=true;
				}
			}else if(i==2){
				if(l[i]==1){
					ok2=true;
					ok3=true;
				}
				if(s[i]==1){
					ok1=true;
					ok3=true;
				}
				if(r[i]==1){
					ok3=true;
					ok4=true;
				}
			}else{
				if(l[i]==1){
					ok3=true;
					ok4=true;
				}
				if(s[i]==1){
					ok2=true;
					ok4=true;
				}
				if(r[i]==1){
					ok1=true;
					ok4=true;
				}
			}
		}
	}

	private static void GetAns() {
		for(int i=0;i<4;i++){
			if(i==0){
				if(p[i]==1&&ok1==true){
					ok=true;
					break;
				}
			}else if(i==1){
				if(p[i]==1&&ok2==true){
					ok=true;
					break;
				}
			}else if(i==2){
				if(p[i]==1&&ok3==true){
					ok=true;
					break;
				}
			}else{
				if(p[i]==1&&ok4==true){
					ok=true;
					break;
				}
			}
		}
		if(ok){
			System.out.println("YES");
		}else{
			System.out.println("NO");
		}

	}

	//a^b
//	private int POW(int a, int b) {
//		int sum = 1;
//		while (b > 0) {
//			if ((b & 1) == 1) {
//				sum = sum * a;
//			}
//			b >>= 1;
//			a *= a;
//		}
//		return sum;
//	}

}

C. Sagheer and Nubian Market
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost ai Egyptian pounds. If Sagheer buysk items with indices x1, x2, ..., xk, then the cost of item xj is axj + xj·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.

Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?

Input

The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the base costs of the souvenirs.

Output

On a single line, print two integers kT — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these ksouvenirs.

Examples
input
3 11
2 3 5
output
2 11
input
4 100
1 2 5 6
output
4 54
input
1 7
7
output
0 0
Note

In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.

In the second example, he can buy all items as they will cost him [5, 10, 17, 22].

In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.


题意:
给你n个物品; 
你可以选购k个物品;则 
每个物品有一个基础价值; 
然后还有一个附加价值; 
即为k*i; 
这里i是这个物品的下标;(1..n中的某个整数); 
然后你有预算S; 
问你最多能买多少个物品ans; 
并求出买ans个物品的最小花费; 
解:直接二分答案(注意爆int)
import java.util.Arrays;
import java.util.Scanner;



/**
 * 作者:张宇翔 创建日期:2017年6月1日 上午11:16:36 描述:ACM
 */

public class Main {

	private static final int Max = (int) (1e5 + 10);
	private static int n;
	private static long S;
	private static long ans;
	private static long []a;
	public static void main(String[] args) {
		InitData();
		GetAns();
	}

	private static void InitData() {
		Scanner cin = new Scanner(System.in);
		a=new long[Max];
		n=cin.nextInt();
		S=cin.nextLong();
		ans=0;
		for(int i=0;i<n;i++){
			a[i]=cin.nextLong();
		}
	}

	private static void GetAns() {
		int l=0,r=n;
		int last=0;
		while(l<=r){
			int mid=(l+r)>>1;
		    if(Check(mid)){
		    	last=mid;
		    	l=mid+1;
		    }else{
		    	r=mid-1;
		    }
		}
		System.out.println(last+" "+ans);
	}
	private static boolean Check(long k){
		long b[]=new long[Max];
		for(int i=0;i<n;i++){
			b[i]=a[i]+(i+1)*k;
		}
		Arrays.sort(b,0,n);
		long sum=0;
		for(int i=0;i<k;i++){
			sum+=b[i];
			if(sum>S){
				return false;
			}
		}
		ans=sum;
		return true;
	}

}