求解滴滴俄罗斯套娃只Ac了百分之90 ,求指点错误

import java.util.*;

/**
 * @since 2017年4月22日
 * @version 1.0
 */

public class s2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int map[][] = new int[n][2];

		for (int i = 0; i < n; i++) {
			map[i][0] = sc.nextInt();
			map[i][1] = sc.nextInt();
		}
		int max = Integer.MIN_VALUE;

		boolean vis[] = new boolean[n];
		int ***[] = new int[n];
		for (int i = 0; i < n; i++) {
			if (!vis[i])
				max = Math.max(dfs(map, vis, i, ***), max);
		}

		if (max != 1)
			max--;
		System.out.println(max);
	}

	private static int dfs(int[][] map, boolean[] vis, int id, int[] ***) {
		if (***[id] != 0)
			return ***[id];
		int co = 0;
		for (int i = 0; i < map.length; i++) {

			if (!vis[i] && i != id 
					&& ((map[i][0] <= map[id][0]&& map[i][1] < map[id][1]) 
							|| (map[i][0] < map[id][0] && map[i][1] <= map[id][1]))) {
				vis[i] = true;
				co = Math.max(co, dfs(map, vis, i, ***));

			}
		}
		***[id] = co + 1;
		return co + 1;
	}

}


全部评论
手写个快排。然后按宽排一次,在宽的基础上按高排一次不就行了?
点赞 回复 分享
发布于 2017-04-22 22:57
A了50%的飘过= =
点赞 回复 分享
发布于 2017-04-22 21:05
我是重新定义比较规则,进行排序
点赞 回复 分享
发布于 2017-04-22 21:07
AC  
点赞 回复 分享
发布于 2017-04-22 21:09
我也是90%,也是重新排序,但是考虑不出边界值了
点赞 回复 分享
发布于 2017-04-22 21:09
我是C++,最开始也是90%,然后把长宽比较下交换了位置就AC了。
点赞 回复 分享
发布于 2017-04-22 21:09
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int[][] toys = new int[n][2]; for (int i = 0; i < n; i++) { toys[i][0] = sc.nextInt(); toys[i][1] = sc.nextInt(); } Arrays.sort(toys, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { if (o1[0] == o2[0]) return o1[1] <= o2[1] ? -1 : 1; return o1[0] <= o2[0] ? -1 : 1; } }); int count = 1; int i = 0, j = 1; while (j <= n-1) { if (toys[j][0] > toys[i][0] && toys[j][1] > toys[i][1]) { count++; i = j; } j++; } System.out.println(count); } } } 为什么感觉我写的好low,虽然AC了。。
点赞 回复 分享
发布于 2017-04-22 21:11
80%
点赞 回复 分享
发布于 2017-04-22 21:13
package Unfinished; import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet; /* * 直接用一个TreeSet 保存这个对象,重写一下compare方法,保证从小到大的顺序 * 再依次比较计算一下,各个对象x,y值是否递增就ok了 100% */ public class SizeMatch{ private int x ; private int y ; public SizeMatch(int x, int y) { super(); this.x = x; this.y = y; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); TreeSet<SizeMatch> set = new TreeSet<SizeMatch>(new Comparator<SizeMatch>() { @Override public int compare(SizeMatch o1, SizeMatch o2) { if(o1.x<o2.x){ return -1; }else if(o1.x>o2.x){ return 1; }else{ if(o1.y<o2.y){ return -1; }else if(o1.y>o2.y){ return 1; }else{ return 0; } } } }); for(int i=0;i<n;i++){ int x = sc.nextInt(); int y = sc.nextInt(); set.add(new SizeMatch(x, y)); } handler(set); sc.close(); } private static void handler(TreeSet<SizeMatch> set) { int result = 1; SizeMatch fisrt = set.first(); int x= fisrt.x; int y= fisrt.y; for(Object size : set.toArray()){ SizeMatch temp = (SizeMatch) size; if(x<temp.x&&y<temp.y){ result++; x = temp.x; y = temp.y; } } System.out.println(result); } }
点赞 回复 分享
发布于 2017-04-22 21:14
我也来贴一个 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { int n = 0, w = 0, h = 0; cin >> n; vector<pair<int, int>> data; for (int i = 0; i < n; ++i) { cin >> w >> h; data.push_back({ w,h }); } sort(data.begin(), data.end()); vector<int> dp(n, 1); for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { if (data[i].first > data[j].first && data[i].second > data[j].second && dp[i]<dp[j]+1) { dp[i] = dp[j] + 1; } } } auto it = max_element(dp.begin(), dp.end()); cout << *it << endl; return 0; }
点赞 回复 分享
发布于 2017-04-22 21:23
有前端的吗?的只通过了60%。。。
点赞 回复 分享
发布于 2017-04-22 21:28
#include<stdio.h> void quickSort(int a[], int b[],int low, int high) {     int i = low, j = high, temp =a[low],temp2=b[low];     if (low > high) {         return;     }     while (i!=j)     {         while (i<j&&a[j]>=temp)         {             j--;         }         while (i<j&&a[i]<=temp)         {             i++;         }         if (i < j) {             int t = a[i];             a[i] = a[j];             a[j] = t;             t = b[i];             b[i] = b[j];             b[j] = t;         }     }     a[low]= a[i];     b[low] = b[i];     b[i] = temp2;     a[i] = temp;     quickSort(a,b ,low, i - 1);     quickSort(a, b,i + 1, high); } int main() {     int n;     int wa[1000], ha[1000];     int w, h;     scanf_s("%d", &n);     for (int i = 0; i < n; i++) {         scanf_s("%d%d", &w, &h);         wa[i]= w;         ha[i]= h;     }     quickSort(wa,ha,0, n-1);     int i = 0;     int count =1;     for (int j = i + 1; j < n; j++) {         if (wa[i] < wa[j] && ha[i] < ha[j]) {             count++;             i = j;         }     }     printf("%d\n", count); }
点赞 回复 分享
发布于 2017-04-22 21:38
不是手动写个排序就过去了吗= =
点赞 回复 分享
发布于 2017-04-22 21:43
NSGA-II非劣排序
点赞 回复 分享
发布于 2017-04-23 01:18
AC 通过 #include <iostream> #include <string> #include <vector> #include <algorithm> #include <map> using namespace std; struct Recta { int width; int height; Recta(int _w = 0, int _h = 0) :width(_w), height(_h) {}; }; bool m_comp(const Recta& a, const Recta& b) { if (a.width == b.width) { return a.height < b.height; } else { return a.width < b.width; } } int maxCount(int number, vector<Recta> datas) { int res = 1; int outW, outH; sort(datas.begin(), datas.end(), m_comp); //按照width升序排序 outW = datas[0].width; outH = datas[0].height; for (int i = 1; i < number; i++) { if (datas[i].width > outW && datas[i].height > outH) { res++; outW = datas[i].width; outH = datas[i].height; } } return res; } int main() { int number; Recta temp; while (cin>>number) { vector<Recta> datas(number); for (int i = 0; i < number; i++) { cin >> temp.width >> temp.height; datas[i] = temp; } cout << maxCount(number,datas) << endl; } //system("pause"); return 0; }
点赞 回复 分享
发布于 2017-04-23 07:59
同AC struct node {     int x,y;     bool operator<(const node& a)const     {         return x < a.x || (x == a.x && y < a.y);     } }a[55]; int n; int dp[55]; int main() { while(scanf("%d",&n) != EOF)     {         for(int i = 0; i < n; i++)             scanf("%d%d",&a[i].x,&a[i].y);         sort(a,a+n);         int ans = 0;         dp[0] = 1;         for(int i = 1; i < n; i++)         {             dp[i] = 1;             for(int j = 0; j < i; j++)                 if(a[j].x < a[i].x && a[j].y < a[i].y)                     dp[i] = max(dp[i],dp[j]+1);             ans = max(ans,dp[i]);         }         printf("%d\n",ans);     } return 0; }
点赞 回复 分享
发布于 2017-04-23 09:48

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务